Solved! Go to Solution.
import csv in_file = "hello.csv" out_file = "hello_fixed.csv" row_reader = csv.reader(open(in_file, "rb")) row_writer = csv.writer(open(out_file, "wb")) first_row = row_reader.next() row_writer.writerow(first_row) for row in row_reader: new_row = [val if val else "N" for val in row] + (["N"] * (len(first_row) - len(row))) print row, "->", new_row row_writer.writerow(new_row)
well you are telling the line to replace " " with "N". Try "" instead of " ". This is replacing an empty string instead of a string that contains only a space character.
import csv in_file = "hello.csv" out_file = "hello_fixed.csv" row_reader = csv.reader(open(in_file, "rb")) row_writer = csv.writer(open(out_file, "wb")) first_row = row_reader.next() row_writer.writerow(first_row) for row in row_reader: new_row = [val if val else "N" for val in row] + (["N"] * (len(first_row) - len(row))) print row, "->", new_row row_writer.writerow(new_row)
This won't work, you're going to need to use the CSV module to look deeper into your data. Yo could do something like this:import csv in_file = "hello.csv" out_file = "hello_fixed.csv" row_reader = csv.reader(open(in_file, "rb")) row_writer = csv.writer(open(out_file, "wb")) first_row = row_reader.next() row_writer.writerow(first_row) for row in row_reader: new_row = [val if val else "N" for val in row] + (["N"] * (len(first_row) - len(row))) print row, "->", new_row row_writer.writerow(new_row)
Even though this is plain text, it's pretty tricky to get right without the help of a library.
I mean Python *is* great but I want to add to the thread that the import wizard in MS Excel is pretty flexible with a lot of delimited (and fixed-column) data -- so there is absolutely nothing wrong with using the excel text file wizard to get to excel, fix your data up, and export again as csv for import to ArcGIS. Just sayin.