0

I have a list that has this form

[['url', 'date', 'extractRaw', 'extractClean'], ['https://www.congress.gov/crec/2017/01/09/CREC-2017-01-09-senate.pdf', '20170109', 'UR\n\nIB\nU\n\nU\n\nE PL\n\nNU\n\nCo', '20170109', 'URIBUUE PLNUCo'], ['https://www.congress.gov/crec/2017/01/09/CREC-2017-01-09-senate.pdf', '20170109', 'UR\n\nIB\nU\n\nU\n\nE PL\n\nNU\n\nCo', '20170109', 'UURIBUUE PLNUCo']]

I'm exporting it to a CSV with this code

def exportCSV(flatList, filename):
    with open(filename+".csv", "wb") as f:
        writer = csv.writer(f)
        writer.writerows(flatList)

exportCSV(textExport,'textExport')

This version follows the new line and I end up with a CSV that reads to a new line for every one of the \n symbols.

My desire is to get each entry in the list on its own separate line. It would look something like this

url    date     extractRaw    extractClean
https://www.congress.gov/crec/2017/01/09/CREC-2017-01-09-senate.pdf    20170109    UR\n\nIB\nU\n\nU\n\nE PL\n\nNU\n\nCo    URIBUUE PLNUCo
https://www.congress.gov/crec/2017/01/09/CREC-2017-01-09-senate.pdf    20170109    UR\n\nIB\nU\n\nU\n\nE PL\n\nNU\n\nCo    URIBUUE PLNUCo
https://www.congress.gov/crec/2017/01/09/CREC-2017-01-09-senate.pdf    20170109    UR\n\nIB\nU\n\nU\n\nE PL\n\nNU\n\nCo    URIBUUE PLNUCo

Does writer.writerows() support that? Can I get it to ignore the new line symbols?

It's not a duplicate. The \n is part of the block of text and the file is opened as 'wb'.

sophros
  • 14,672
  • 11
  • 46
  • 75
Sebastian
  • 957
  • 3
  • 15
  • 27
  • Possible duplicate of [CSV in Python adding an extra carriage return, on Windows](https://stackoverflow.com/questions/3191528/csv-in-python-adding-an-extra-carriage-return-on-windows) – Rietty Feb 03 '19 at 22:58
  • The Python 3 documentation says to open files for output with `open(filename, mode='w', newline='')`. Try that. – martineau Feb 03 '19 at 23:03
  • I should've mentioned it's python 2.7. It looks like newline didn't exist for 2.7 – Sebastian Feb 03 '19 at 23:09
  • On the chance that you have the option, you could export to a more capable format, like .xlsx or .ods. – Tom Blodget Feb 04 '19 at 05:41

1 Answers1

0

As you noticed, some of your strings contain \n chars.

"\n" (or ASCII 0x0A (10) or LF (line feed)), is a special char that gets interpreted when such a string is written. In order to solve your problem, you could either:

  • Make your strings raw (don't know how feasible is that). Anyway for more details check [Python 2]: String literals
  • Manually escape any "\" (bkslash) character preceding "n", so that "\n" becomes a string that consists of 2 chars ("\" and "n"). Translated into code, you should replace your last line, by (assuming that textExport contents was pasted at the beginning):

    escapedTextExport = [[item.replace("\n", "\\n") for item in row] for row in textExport]
    
    exportCSV(escapedTextExport, "textExport")
    
CristiFati
  • 38,250
  • 9
  • 50
  • 87