0

With the following (note the first row has no leading space):

Test1@bigfoot.com
 Test11@bigfoot.com
 Test1111@bigfoot.com
 Test111ew@bigfoot.com
 Test12312@bigfoot.com
 Test1231321@bigfoot.com
 Test1342321@bigfoot.com
 ....
 481 total rows

The following correctly removes the leading space, but inserts a blank row after each string row, AND, truncates the total list by a random number of rows each time it is executed.

csvfile= open('list.csv','r')
csvfile1= open('complete_list.csv','w')
stripped = (row.strip() for row in csvfile)
reader = csv.reader(stripped,delimiter=' ')
writer= csv.writer(csvfile1)
for row in reader:
    writer.writerow([e.strip() for e in row])

And:

with open('list.csv') as infile:
    reader = csv.DictReader(infile)
    fieldnames = reader.fieldnames
    for row in reader:
        row.update({fieldname: value.strip() for (fieldname, value) in row.items()})

Does nothing, as the first row is assumed to be the fieldname, when in fact it is simply...a row.

tpcolson
  • 716
  • 1
  • 11
  • 27

1 Answers1

1

several issues here:

  • csv files must be opened in write mode with newline="" in python 3, else it insert blanks on windows
  • don't use strip but lstrip on the lines, else it removes newline at the end of the line. Can confuse csv reader
  • use with context block so it ensures that the files are closed when exiting the block (should handle your random missing lines in the end)

my proposal:

with open('list.csv','r') as csvfile, open('complete_list.csv','w',newline="") as csvfile1:  # newline="" to avoid blanks
    stripped = (row.lstrip() for row in csvfile)  # lstrip not strip
    reader = csv.reader(stripped,delimiter=' ')
    writer= csv.writer(csvfile1)
    writer.writerows(reader)   # don't overstrip: just write rows as-is
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • Ahh that did it! I was drawing a complete blank on the correct order of closing/opening preparatory to writing thanks! – tpcolson Dec 26 '17 at 18:21