Python脚本在读取数据的同时将数据移至下一行

时间:2018-11-11 03:15:46

标签: python csv

我写了一个代码,计算一行中的分隔符数量,如果一行中存在的分隔符数量多于或少于每行预期的分隔符数量,那么该行将被打印并复制到另一行文件(Lines_FILE.txt)进行分析。例如:

1,a,b,c,d
2,e,f,g,h
3,r,h,,u,j

第三行以上将被复制并粘贴到新文件中。脚本为:

import string

### PLEASE  DELETE THE FILE "Lines_FILE.txt" BEFORE RUNNING THIS SCRIPT

k = 0
linecount=0

with open('Mock.txt',encoding="latin1") as myfile:  #input file name with extension also if required update file encoding
    for line in myfile:
        k=0
        linecount=linecount+1
        words = line.split()
        for i in words:
            for letter in i:
                    #k=line.count('"|"')  #Unhash and Update delimiter and Text Qualifier if text qualifier present
                    k=line.count(',')    #Unhash and Update delimiter if no text qualifier
        print("Lines:",linecount)
        print(k)
        if(k!=94):  #Update the number of delimiters present in the first line or the expected delimiters per line.
            print(line)
            f = open("Lines_FILE.txt","a")
            f.write(line)

工作正常,但突然我注意到一个文件,脚本拾取了没有错误的行并将其粘贴到Lines_FILE.txt中。我注意到该脚本起了一行 在Lines_FILE.txt文件中,将一半的行移至下一行,而在实际数据中则并非如此。这是一行:

10804395,1,10/4/2018 6:45:27 PM,742443,23,2122804,OCT-18,10/4/2018,P,10/4/2018 6:44:34 PM,742443,,,2779094.44,,2779094.44,Reclass since no Physical inventory with Sanmina    ,,,,,,,,,JE_AUTO_FILE_renurana_Sep-18_11_6720973_10-04-2018_104704_36,,,,,,,,,,,,,,,,,,Manual JE File Name,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,2
10804396,1,10/4/2018 6:45:27 PM,742443,23,2122805,OCT-18,10/4/2018,P,10/4/2018 6:44:35 PM,742443,,235530.26,,235530.26,,Fresh billing to Jabil against sanmina inventory movement reconciled to open POs from Jabil    ,,,,,,,,,JE_AUTO_FILE_renurana_Sep-18_11_6720973_10-04-2018_104704_36,,,,,,,,,,,,,,,,,,Manual JE File Name,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,2

提取的行看起来像:

10804395,1,10/4/2018 6:45:27 PM,742443,23,2122804,OCT-18,10/4/2018,P,10/4/2018 6:44:34 PM,742443,,,2779094.44,,2779094.44,Reclass since no Physical inventory with Sanmina
,,,,,,,,,JE_AUTO_FILE_renurana_Sep-18_11_6720973_10-04-2018_104704_36,,,,,,,,,,,,,,,,,,Manual JE File Name,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,2
10804396,1,10/4/2018 6:45:27 PM,742443,23,2122805,OCT-18,10/4/2018,P,10/4/2018 6:44:35 PM,742443,,235530.26,,235530.26,,Fresh billing to Jabil against sanmina inventory movement reconciled to open POs from Jabil
,,,,,,,,,JE_AUTO_FILE_renurana_Sep-18_11_6720973_10-04-2018_104704_36,,,,,,,,,,,,,,,,,,Manual JE File Name,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,2

在“ with Sanmina”和“ Jabil”文本之后,该行被推到下一行。我注意到同一模式出现了几行。我想这与那些文本之后的空白有关系。 总结一下问题,脚本在读取数据时会中断几行并将其视为错误行。作为python的新手,如果有人可以指导我解决此问题,那将是非常有用的。

1 个答案:

答案 0 :(得分:0)

原因可能是您处理两个文件的方式不同。第一个文件采用特定的编码,第二个文件为默认编码。我可以对您使用的脚本进行一些改进。

line_no = 1
with open("Mock.txt", "r", encoding="latin1") as infile:
  with open("Lines_FILE.txt", "w", encoding="latin1") as outfile:
    for line in infile:
      delim_count = line.count(",")
      print("Line: ", line_no)
      if delim_count != 94:
        print(line)
        outfile.write(line)

这应该以相同的编码方式读写文件。