Danny Garcia
Danny Garcia

Reputation: 227

Splitting a ".txt" file

I have data in a .txt file, in the form a of a comma separated list. For example:

N12345678,B,A,D,D,C,B,D,A,C,C,D,B,A,B,A,C,B,D,A,C,A,A,B,D,D
N12345678,B,A,D,D,C,B,D,A,C,C,D,B,A,B,A,C,B,D,A,C,A,A,B,D,D

I want to be able to split it up, first by line, then by comma, so I'm able to process the data and validate it. I am getting "invalid" for all of lines in my code, even though some of them should be valid because there should be 26 characters per line. Here is my code so far:

(filename+".txt").split("\n")
(filename+".txt").split(",")
with open(filename+".txt") as f:
  for line in f:
      if len(line) != 26:
          print ("invalid")
      else:
          print ("valid")

Upvotes: 0

Views: 99

Answers (3)

Ahasanul Haque
Ahasanul Haque

Reputation: 11134

As far as I understand your question, you want this.

with open(filename, 'r') as f:
    for line in f:
        if len(line.split(',')) !=26:
            print("Invalid")
        else:
            print("Valid")

All it does is,

  • Open the file.
  • Read the file line by line.
  • For each line, split the line by ,
  • As str.split() returns a list, check if length of the list is 26 or not.
  • If length is 26, count as valid; otherwise not.

Upvotes: -1

Ashish
Ashish

Reputation: 266

I think an easier way to do this would be to use csv module.

import csv

with open("C:/text.csv") as input:
    reader = csv.reader(input)
    for row in reader:
        if len(row) == 26:
            print("Valid")
        else:
            print("Invalid")

Upvotes: 1

TessellatingHeckler
TessellatingHeckler

Reputation: 28993

This code is quite far from working; it's syntactically valid Python, but it doesn't mean anything sensible.

# These two lines add two strings together, returning a string
# then they split the string into pieces into a list
# because the /filename/ has no newlines in it, and probably no commas
# that changes nothing
# then the return value isn't saved anywhere, so it gets thrown away
(filename+".txt").split("\n")
(filename+".txt").split(",")

# This opens the file and reads from it line by line,
# which means "line" is a string of text for each line in the file.
with open(filename+".txt") as f:
  for line in f:

      # This checks if the line in the file is not the /number/ 26
      # since the file contains /strings/ it never will be the number 26
      if line != 26:
          print ("invalid")

      # so this is never hit
      else:
          print ("valid")

[Edit: even in your updated code, the line is the whole text "N12345678,B,A,D..." and because of the commas, len(line) will be longer than 26 characters.]

It seems you want something more like: Drop the first two lines of your code completely, read through the file line by line (meaning you normally don't have to care about "\n" in your code). Then split each line by commas.

with open(filename+".txt") as f:
  for line in f:
      line_parts = line.split(",")
      if len(line_parts) != 26:
          print ("invalid")
      else:
          print ("valid")
          # line_parts is now a list of strings
          # ["N12345678" ,"B", "A", ...]

Upvotes: 3

Related Questions