Python write csv file line by line

I have data which is being accessed via http request and is sent back by the server in a comma separated format, I have the following code :

site= 'www.example.com'
hdr = {'User-Agent': 'Mozilla/5.0'}
req = urllib2.Request[site,headers=hdr]
page = urllib2.urlopen[req]
soup = BeautifulSoup[page]
soup = soup.get_text[]
text=str[soup]

The content of text is as follows:

april,2,5,7
may,3,5,8
june,4,7,3
july,5,6,9

How can I save this data into a CSV file. I know I can do something along the lines of the following to iterate line by line:

import StringIO
s = StringIO.StringIO[text]
for line in s:

But i'm unsure how to now properly write each line to CSV

EDIT---> Thanks for the feedback as suggested the solution was rather simple and can be seen below.

Solution:

import StringIO
s = StringIO.StringIO[text]
with open['fileName.csv', 'w'] as f:
    for line in s:
        f.write[line]

Summary: in this tutorial, you’ll learn how to write data into a CSV file using the built-in csv module.

Steps for writing a CSV file

To write data into a CSV file, you follow these steps:

  • 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.
  • Finally, close the file once you complete writing data to it.

The following code illustrates the above steps:

import csv # open the file in the write mode f = open['path/to/csv_file', 'w'] # create the csv writer writer = csv.writer[f] # write a row to the csv file writer.writerow[row] # close the file f.close[]

Code language: Python [python]

It’ll be shorter if you use the with statement so that you don’t need to call the close[] method to explicitly close the file:

import csv # open the file in the write mode with open['path/to/csv_file', 'w'] as f: # create the csv writer writer = csv.writer[f] # write a row to the csv file writer.writerow[row]

Code language: PHP [php]

If you’re dealing with non-ASCII characters, you need to specify the character encoding in the open[] function.

The following illustrates how to write UTF-8 characters to a CSV file:

import csv # open the file in the write mode with open['path/to/csv_file', 'w', encoding='UTF8'] as f: # create the csv writer writer = csv.writer[f] # write a row to the csv file writer.writerow[row]

Code language: PHP [php]

Writing to CSV files example

The following example shows how to write data to the CSV file:

import csv header = ['name', 'area', 'country_code2', 'country_code3'] data = ['Afghanistan', 652090, 'AF', 'AFG'] with open['countries.csv', 'w', encoding='UTF8'] as f: writer = csv.writer[f] # write the header writer.writerow[header] # write the data writer.writerow[data]

Code language: PHP [php]

If you open the countries.csv, you’ll see one issue that the file contents have an additional blank line between two subsequent rows:

To remove the blank line, you pass the keyword argument newline='' to the open[] function as follows:

import csv header = ['name', 'area', 'country_code2', 'country_code3'] data = ['Afghanistan', 652090, 'AF', 'AFG'] with open['countries.csv', 'w', encoding='UTF8', newline=''] as f: writer = csv.writer[f] # write the header writer.writerow[header] # write the data writer.writerow[data]

Code language: PHP [php]

Output:

Writing multiple rows to CSV files

To write multiple rows to a CSV file at once, you use the writerows[] method of the CSV writer object.

The following uses the writerows[] method to write multiple rows into the countries.csv file:

import csv header = ['name', 'area', 'country_code2', 'country_code3'] data = [ ['Albania', 28748, 'AL', 'ALB'], ['Algeria', 2381741, 'DZ', 'DZA'], ['American Samoa', 199, 'AS', 'ASM'], ['Andorra', 468, 'AD', 'AND'], ['Angola', 1246700, 'AO', 'AGO'] ] with open['countries.csv', 'w', encoding='UTF8', newline=''] as f: writer = csv.writer[f] # write the header writer.writerow[header] # write multiple rows writer.writerows[data]

Code language: PHP [php]

Writing to CSV files using the DictWriter class

If each row of the CSV file is a dictionary, you can use the DictWriter class of the csv module to write the dictionary to the CSV file.

The example illustrates how to use the DictWriter class to write data to a CSV file:

import csv # csv header fieldnames = ['name', 'area', 'country_code2', 'country_code3'] # csv data rows = [ {'name': 'Albania', 'area': 28748, 'country_code2': 'AL', 'country_code3': 'ALB'}, {'name': 'Algeria', 'area': 2381741, 'country_code2': 'DZ', 'country_code3': 'DZA'}, {'name': 'American Samoa', 'area': 199, 'country_code2': 'AS', 'country_code3': 'ASM'} ] with open['countries.csv', 'w', encoding='UTF8', newline=''] as f: writer = csv.DictWriter[f, fieldnames=fieldnames] writer.writeheader[] writer.writerows[rows]

Code language: PHP [php]

How it works.

  • First, define variables that hold the field names and data rows of the CSV file.
  • Next, open the CSV file for writing by calling the open[] function.
  • Then, create a new instance of the DictWriter class by passing the file object [f] and fieldnames argument to it.
  • After that, write the header for the CSV file by calling the writeheader[] method.
  • Finally, write data rows to the CSV file using the writerows[] method.

Summary

  • Use the CSV Writer or the DictWriter class to write data to a CSV file.

Did you find this tutorial helpful ?

How do you write multiple lines in a CSV file in Python?

How to Write Multiple Rows into 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 write a CSV file in Python?

Python Write 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 read a CSV file in a line by line in Python?

Using reader.
Step 1: In order to read rows in Python, First, we need to load the CSV file in one object. So to load the csv file into an object use open[] method..
Step 2: Create a reader object by passing the above-created file object to the reader function..
Step 3: Use for loop on reader object to get each row..

How do I write to column wise in a CSV file in Python?

Steps will be to append a column in csv file are,.
Open 'input.csv' file in read mode and create csv.reader object for this csv file..
Open 'output.csv' file in write mode and create csv.writer object for this csv file..
Using reader object, read the 'input.csv' file line by line. ... .
Close both input..

Chủ Đề