📜  python csv writer row by row - Python (1)

📅  最后修改于: 2023-12-03 15:33:58.851000             🧑  作者: Mango

Python CSV Writer Row by Row

Python is a powerful and flexible language that can be used to manipulate and work with a variety of data formats. One common task in working with data is writing data to a CSV file. In this tutorial, we'll explore how to use Python to write data to a CSV file row by row.

Writing CSV Files in Python

Python provides a built-in module called csv that makes it easy to write data to CSV files. To write CSV files row by row, we will use the csv.writer object.

Here's an example of creating a csv.writer object and writing a single row to a CSV file:

import csv

# Create a new CSV file
with open('myfile.csv', mode='w') as file:
    writer = csv.writer(file)

    # Write a row to the CSV file
    writer.writerow(['Name', 'Age', 'City'])

    # Write another row to the CSV file
    writer.writerow(['John', 25, 'New York'])

In this example, we first import the csv module, which we'll need to work with CSV files.

Next, we create a new CSV file using the open() function and the mode='w' parameter, which means we want to open the file for writing. We also create a csv.writer object called writer, which we'll use to write data to the CSV file.

We then use the writerow() method to write a single row to the CSV file. The writerow() method takes a list of values, which represent the values in each column of the CSV file. In this case, we're writing three values: 'Name', 'Age', and 'City'.

We then use writerow() again to write another row to the CSV file, this time with the values 'John', 25, and 'New York'.

Writing Multiple Rows to a CSV File

So far, we've only written one row to our CSV file. To write multiple rows to the file, we can simply call writerow() multiple times, each time with a different list of values.

Here's an example of writing multiple rows to a CSV file:

import csv

# Create a new CSV file
with open('myfile.csv', mode='w') as file:
    writer = csv.writer(file)

    # Write the header row
    writer.writerow(['Name', 'Age', 'City'])

    # Write multiple data rows
    writer.writerow(['John', 25, 'New York'])
    writer.writerow(['Lisa', 32, 'Los Angeles'])
    writer.writerow(['Bob', 41, 'Chicago'])

In this example, we start by creating a new CSV file, just like in the previous example. We also create a csv.writer object called writer.

We then write the header row to the CSV file using writerow(). This time, we're specifying the column headers explicitly: 'Name', 'Age', and 'City'.

Next, we write multiple data rows to the CSV file, each with a different set of values.

Writing Rows from a List of Lists

Rather than writing rows explicitly with writerow(), we can also write multiple rows at once by passing in a list of lists. Each inner list represents a row of data, and each value in the inner list represents a column value.

Here's an example of writing rows from a list of lists:

import csv

# Define a list of lists
data = [
    ['Name', 'Age', 'City'],
    ['John', 25, 'New York'],
    ['Lisa', 32, 'Los Angeles'],
    ['Bob', 41, 'Chicago']
]

# Create a new CSV file and write the data
with open('myfile.csv', mode='w') as file:
    writer = csv.writer(file)
    writer.writerows(data)

In this example, we define a list of lists called data. The first item in the list represents the header row, and each subsequent item represents a data row.

We then create a new CSV file using open() and csv.writer(), and we call writer.writerows(data) to write all of the rows to the CSV file at once.

Conclusion

In this tutorial, we've explored how to use Python to write data to a CSV file row by row. We've seen how to create a csv.writer object, write a single row, write multiple rows, and write rows from a list of lists. With these techniques, you can write Python scripts to extract, manipulate, and write data to CSV files with ease.