I asked this question a few days ago because I needed help with editing some CSV files: Fix numbering on CSV files that have deleted lines.
The people of Stack Overflow helped me out a great deal however I keep getting an error saying AttributeError: 'int' object has no attribute 'strip'.
The problem is that all the information in my CSV files are not integers. Being the python newbie that I am, a few days of trying to fix it only made things worse. Here is what I have from my previous question that gives me the error:
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)
The code is essentially supposed to run through a bunch of CSV files and delete all the lines that have 'DIF' in them and fix the numbering due to deleted lines. Does anyone have any suggestions?