Python csv.writerow () acts like tad funky

From what I explored, csv.writeRow should take in the list and then write it to the given csv file. Here is what I tried:

from csv import writer with open('Test.csv', 'wb') as file: csvFile, count = writer(file), 0 titles = ["Hello", "World", "My", "Name", "Is", "Simon"] csvFile.writerow(titles) 

I'm just trying to write it so that each word is in a different column.

When I open the file that it creates, I get the following message:

enter image description here

After continuous clicking, I get a message that the file is either damaged or the SYLK file. Then I can open the file, but only after going through two error messages every time I open the file.

Why is this?

Thanks!

+7
python csv
source share
1 answer

This is a documented issue that Excel will consider a csv file is SYLK if the first two characters are identifiers.

Speaking in the field of opinions - this should not, but Excel believes that it knows better than the extension. In fairness, we note that people expect to be able to identify cases where the extension is really erroneous, but in this case, assuming the extension is incorrect, and then assuming that the file is corrupted when it does not look corrupted if interpreted in keeping up with the extension is just staggering.

@John Y points out:

One thing you need to pay attention to: The "workaround" posed by the Microsoft problem associated with @PeterDeGlopper is to (manually) add an apostrophe to the file. (These are also tips commonly found on the Internet, including StackOverflow, to try to get CSV digits to be treated as strings, not numbers.) This is not what I would call good advice, as it introduces a literal apostrophe into your data.

@DSM suggests using quoting=csv.QUOTE_NONNUMERIC for writing. Excel is not confused with a file starting with "ID" and not with ID , so if other tools that work with CSV are consistent with this citation level, this is probably the best solution, except to simply ignore the confusion of Excel.

+14
source share

All Articles