I wrote a small script in Python to accept a csv file similar to what comes from Excel and output it as a pipe delimited file. When encountering a cell containing multiple lines, it currently adds a backslash (as that is what I specified as the escape character) at the end of the line and continues the cell on the next line. What I want to do though is be able to specify a space character or a string that the new line would be replaced with instead of the backslash and continue the record on the same line. I am having some trouble accomplishing this though. Is there an easy way to do this using the csv module? What I have so far:
fout = open (tfile, "wt")
cout = csv.writer(fout, delimiter = '|', quotechar = '', quoting = csv.QUOTE_NONE, lineterminator = '\n', escapechar='\\')
cin = csv.reader(fin)
for row in cin:
I worked out the answer to my issue. Instead of using a backslash as the escape character, I use '\n' and then do a find and replace of the new line for every field in each row. I.e.
cout = csv.writer(fout, delimiter = '|', quotechar = '', quoting = csv.QUOTE_NONE, lineterminator = '\n', escapechar='\n')
for row in cin: newrow = row row[:] = [str.replace("\n", " ") for str in row] cout.writerow(newrow)
The find and replace of the list is based on the response to How to modify list entries during for loop?