拆分“.txt”文件

时间:2016-05-02 03:56:48

标签: python file split

我在.txt文件中有数据,格式为逗号分隔列表。例如:

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

我希望能够先将其拆分,然后再按行,然后使用逗号,这样我就可以处理数据并对其进行验证。我对代码中的所有行都“无效”,即使其中一些行应该有效,因为每行应该有26个字符。到目前为止,这是我的代码:

(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")

3 个答案:

答案 0 :(得分:3)

这段代码离工作还很远;它具有语法上有效的Python,但它并不是意味着任何合理的。

# 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")

[编辑:即使在更新的代码中,line也是全文"N12345678,B,A,D...",由于逗号,len(行)将超过26个字符。]

似乎你想要更像的东西:完全删除代码的前两行,逐行阅读文件(意味着你通常不必关心" \ n"在你的代码中)。然后用逗号分隔每个

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", ...]

答案 1 :(得分:1)

我认为更简单的方法是使用csv模块。

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")

答案 2 :(得分:-1)

据我了解你的问题,你想要这个。

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

所有这一切都是,

  • 打开文件。
  • 逐行阅读文件。
  • 对于每一行,请按,
  • 拆分该行
  • str.split()返回一个列表时,检查列表的长度是否为26。
  • 如果长度为26,则视为有效;否则没有。
相关问题