0

几天前我问了这个问题,因为我在编辑一些 CSV 文件时需要帮助:修复已删除行的 CSV 文件的编号。Stack Overflow 的人帮了我很多,但是我一直收到一个错误说AttributeError: 'int' object has no attribute 'strip'.问题是我的 CSV 文件中的所有信息都不是整数。作为我的 python 新手,尝试修复它几天只会让事情变得更糟。这是我之前的问题中给出的错误信息:

import csv
import glob
import os 
import re 

numbered = re.compile(r'N\d+').match

for fn in fns:
     # open for counting
     reader = csv.reader(open(fn,"rb"))
     count = sum(1 for row in reader if row and not any(r.strip() == 'DIF' for r in     row) and numbered(row[0]))

 # reopen for filtering
 reader = csv.reader(open(fn,"rb"))

 with open (os.path.join('out', fn), 'wb') as f:
    counter = 0
    w = csv.writer(f)
    for row in reader:
        if row and 'Count' in row[0].strip():
            row = ['Count', count]
        if row and not any(r.strip() == 'DIF' for r in row): #remove DIF
            if numbered(row[0]):
                counter += 1
                row[0] = 'N%d' % counter
        w.writerow(row)

该代码本质上应该运行一堆 CSV 文件并删除所有包含“DIF”的行并修复由于删除行而导致的编号。有没有人有什么建议?

4

1 回答 1

0

最简单的可能是将 r 包装在 str() 中。但同时,你为什么不只读一次文件,让它更容易:

import csv
import glob
import os
import re

numbered = re.compile(r'N\d+').match

for fn in fns:
     reader = csv.reader(open(fn,"rb"))

     # filter out 'DIF' rows here
     rows = [ row for row in reader 
              if not any(str(r).strip() == 'DIF' 
                   for r in row) ]

     # count numbered rows
     count = len([row for row in rows if row and numbered(row[0])])

     with open (os.path.join('out', fn), 'wb') as f:
        counter = 0
        w = csv.writer(f)

        for row in rows:
            if row and 'Count' in row[0].strip():
                row = ['Count', count]

            if row and numbered(row[0]):
                counter += 1
                row[0] = 'N%d' % counter

            w.writerow(row)
于 2013-08-22T16:30:36.960 回答