I am using Spyder and accessing the reddit API to get some data and write it to csv, when I print out the lines, everything works fine, but then the csv file just doesn't get created, I tried many things but nothing seems to work, and a very similar piece of code worked out fine and I got the csv file, so I have no idea what the problem is. Show
asked Jul 4, 2013 at 11:20
2 Your CSV is created, but you are using a relative file path. You'd have to print Best to use an absolute file path instead:
answered Jul 4, 2013 at 11:22
Martijn Pieters♦Martijn Pieters 988k274 gold badges3887 silver badges3244 bronze badges 3 I turned your code into a self-contained example:
It works fine:
So your problem is not with the code you've shown here. answered Jul 4, 2013 at 11:24
John ZwinckJohn Zwinck 228k33 gold badges306 silver badges421 bronze badges 2 We are going to exclusively use the
We have already covered the basics of how to use the Basic Usage of csv.writer()Let's look at a basic example of using Example 1: Write into CSV files with csv.writer()Suppose we want to write a CSV file with the following entries: SN,Name,Contribution 1,Linus Torvalds,Linux Kernel 2,Tim Berners-Lee,World Wide Web 3,Guido van Rossum,Python Programming Here's how we do it.
When we run the above program, an innovators.csv file is created in the current working directory with the given entries. Here, we have opened the
innovators.csv file in writing mode using To learn more about opening files in Python, visit: Python File Input/Output Next, the Example 2: Writing Multiple Rows with writerows()If we need to write the contents of the 2-dimensional list to a CSV file, here's how we can do it.
The output of the program is the same as in Example 1. Here, our 2-dimensional list is passed to the Now let's see how we can write CSV files in different formats. We will then learn how to customize the CSV Files with Custom DelimitersBy default, a comma is used as a delimiter in a CSV file. However, some CSV files can use delimiters other than a comma. Few popular ones are Suppose we want to use Let's take an example. Example 3: Write CSV File Having Pipe Delimiter
Output SN|Name|Contribution 1|Linus Torvalds|Linux Kernel 2|Tim Berners-Lee|World Wide Web 3|Guido van Rossum|Python Programming As we can see, the optional parameter CSV files with QuotesSome CSV files have quotes around each or some of the entries. Let's take quotes.csv as an example, with the following entries: "SN";"Name";"Quotes" 1;"Buddha";"What we think we become" 2;"Mark Twain";"Never regret anything that made you smile" 3;"Oscar Wilde";"Be yourself everyone else is already taken" Using In order to add them, we will have to use another optional
parameter called Let's take an example of how quoting can be used around the non-numeric values and Example 4: Write CSV files with quotes
Output "SN";"Name";"Quotes" 1;"Buddha";"What we think we become" 2;"Mark Twain";"Never regret anything that made you smile" 3;"Oscar Wilde";"Be yourself everyone else is already taken" Here, the quotes.csv file is created in the working directory with the above entries. As you can see, we have passed
There are 3 other predefined constants you can pass to the
CSV files with custom quoting characterWe can also write CSV files with custom quoting characters. For that, we will have to use an optional parameter called Let's take an example of writing quotes.csv file in Example 4, but with Example 5: Writing CSV files with custom quoting character
Output *SN*;*Name*;*Quotes* 1;*Buddha*;*What we think we become* 2;*Mark Twain*;*Never regret anything that made you smile* 3;*Oscar Wilde*;*Be yourself everyone else is already taken* Here, we can see that Dialects in CSV moduleNotice in Example 5 that we have passed multiple parameters ( This practice is acceptable when dealing with one or two files. But it will make the code more redundant and ugly once we start working with multiple CSV files with similar formats. As a solution to this, the Dialect helps in grouping together many specific formatting patterns like It can then be passed as a parameter to multiple Example 6: Write CSV file using dialectSuppose we want to write a CSV file (office.csv) with the following content: "ID"|"Name"|"Email" "A878"|"Alfonso K. Hamby"|"" "F854"|"Susanne Briard"|"" "E833"|"Katja Mauer"|"" The CSV file has quotes around each entry and uses Instead of passing two individual formatting patterns, let's look at how to use dialects to write this file.
Output "ID"|"Name"|"Email" "A878"|"Alfonso K. Hamby"|"" "F854"|"Susanne Briard"|"" "E833"|"Katja Mauer"|"" Here, office.csv is created in the working directory with the above contents. From this example, we can see that the
The custom dialect requires a name in the form of a string. Other specifications can be done either by passing a sub-class of the While creating the The advantage of using Write CSV files with csv.DictWriter()The objects of The minimal syntax of the
Here,
Example 7: Python csv.DictWriter()
Output The program creates a players.csv file with the following entries: player_name,fide_rating Magnus Carlsen,2870 Fabiano Caruana,2822 Ding Liren,2801 The full syntax of the
To learn more about it in detail, visit: Python csv.DictWriter() class CSV files with lineterminatorA However, the doublequote & escapechar in CSV moduleIn order to separate
delimiter characters in the entries, the So, if you had an entry: He is a strong, healthy man, it will be written as: "He is a strong, healthy man". Similarly, the If you had an entry: Go to "programiz.com", it would be written as: "Go to ""programiz.com""". Here, we can see that each doublequoteIt handles how escapechar
Example 8: Using escapechar in csv writer
Output Book,Quote Lord of the Rings,/"All we have to decide is what to do with the time that is given us./" Harry Potter,/"It matters not what someone is born/, but what they grow to be./" Here, we can see that If it wasn't defined, then, the output would be: Book,Quote Lord of the Rings,"""All we have to decide is what to do with the time that is given us.""" Harry Potter,"""It matters not what someone is born, but what they grow to be.""" Since we allow quoting, the entries with special characters( The remaining quote characters are to escape the actual Note: The csv module can also be used for other file extensions (like: .txt) as long as their contents are in proper structure. Recommended Reading: Read CSV Files in Python How do I create a CSV file in Python?Steps for writing a CSV file
First, open the CSV file for writing ( w mode) by using the open() function. Second, create a CSV writer object by calling the writer() function of the csv module. Third, write data to CSV file by calling the writerow() or writerows() method of the CSV writer object.
How do I create an empty CSV file in Python?Creating an empty CSV file using with open
Pass in the name of the new file and 'w' as the second parameter to make the file writable in order to create an empty csv file using with open. A new csv file is created if the file name doesn't already exist. The content will be changed if the file already exists.
Can you write directly to a CSV file in Python?In Python, you can use the CSV writer's writerows() function to write multiple rows into a CSV file on the same go.
How do I create a .CSV file?Save an Excel spreadsheet as a CSV file. In your Excel spreadsheet, click File.. Click Save As.. Click Browse to choose where you want to save your file.. Select "CSV" from the "Save as type" drop-down menu.. Click Save.. |