Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python's csv.writerow() is acting a tad funky

Tags:

python

csv

From what I've researched, csv.writeRow should take in a list, and then write it to the given csv file. Here's 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, however, I get the following message:

enter image description here

After pressing to continue anyways, I get a message saying that the file is either corrupted, or is a SYLK file. I can then open the file, but only after going through two error messages everytime I open the file.

Why is this?

Thanks!

like image 1000
Cisplatin Avatar asked Aug 07 '13 21:08

Cisplatin


People also ask

Why csv writer adds blank rows?

Why csv writer adds blank rows? The way Python handles newlines on Windows can result in blank lines appearing between rows when using csv. writer . In Python 2, opening the file in binary mode disables universal newlines and the data is written properly.

What is csv format in Python?

A CSV file (Comma Separated Values file) is a type of plain text file that uses specific structuring to arrange tabular data. Because it's a plain text file, it can contain only actual text data—in other words, printable ASCII or Unicode characters. The structure of a CSV file is given away by its name.

Can you query a CSV file in Python?

querycsv -- Query a CSV File. querycsv.py is a Python module and program that allows you to execute SQL code against data contained in one or more comma-separated-value (CSV) files. The output of the SQL query will be displayed on the console by default, but may be saved in a new CSV file.


1 Answers

It's a documented issue that Excel will assume a csv file is SYLK if the first two characters are 'ID'.

Venturing into the realm of opinion - it shouldn't, but Excel thinks it knows better than the extension. To be fair, people expect it to be able to figure out cases where the extension really is wrong, but in a case like this assuming the extension is wrong, and then further assuming the file is corrupt when it doesn't appear corrupt if interpreted according to the extension is just mind-boggling.

@John Y points out:

One thing to watch out for: The "workaround" given by the Microsoft issue linked to by @PeterDeGlopper is to (manually) prepend an apostrophe into the file. (This is also advice commonly found on the Web, including StackOverflow, to try to force CSV digits to be treated as strings rather than numbers.) This is not what I'd call good advice, as that injects a literal apostrophe into your data.

@DSM suggests using quoting=csv.QUOTE_NONNUMERIC on the writer. Excel is not confused by a file beginning with "ID" rather than ID, so if the other tools that are going to work with the CSV accept that quoting level this is probably the best solution other than just ignoring Excel's confusion.

like image 90
Peter DeGlopper Avatar answered Oct 21 '22 16:10

Peter DeGlopper