Write String to a .csv File in Python

In this tutorial, we will learn how to write strings to a .csv file in Python. Comma-separated values (CSV) files are a common file format for transferring and storing data. Python provides a built-in module called csv that allows you to easily work with CSV files.

Write Strings to a New CSV File in Python

To write strings to a new CSV file, you can use the csv module. Here’s how you can do it:

  1. Import the csv module.
  2. Open the file in write mode using the open() function. Use the mode 'w'. This will create a new file if it doesn’t exist or truncate the file if it exists.
  3. Create a CSV writer object using csv.writer().
  4. Write the strings to the file using the writerow() method.
  5. Close the file using the close() method.

Let’s write some sentences to a new CSV file in Python called usa.csv.

import csv

# Open the file in write mode
file = open('usa.csv', 'w', newline='')

# Create a CSV writer object
csv_writer = csv.writer(file)

# Write strings to the file
csv_writer.writerow(["The United States of America is a country consisting of 50 states."])
csv_writer.writerow(["The capital city is Washington, D.C., and the largest city is New York City."])

# Close the file
file.close()

Now, when you open the file, you can see those sentences have been written to the usa.csv file.

Write Strings to an Existing CSV File in Python

If you want to add strings to an existing CSV file, you need to open the file in append mode by using the mode 'a'.

Here’s how you can append strings to an existing CSV file in Python:

# Open the file in append mode
file = open('usa.csv', 'a', newline='')

# Create a CSV writer object
csv_writer = csv.writer(file)

# Write strings to the file
csv_writer.writerow(["The United States is known as the land of opportunities."])
csv_writer.writerow(["English is the primary language spoken in the United States."])

# Close the file
file.close()

Using ‘with’ statement for file handling

It’s good practice to use the with statement when dealing with file objects. This has the advantage that the file is properly closed after its suite finishes executing.

Here’s how you can write to a CSV file using the with statement:

import csv

# Using with statement to write to a new file
with open('usa_new.csv', 'w', newline='') as file:
    csv_writer = csv.writer(file)
    csv_writer.writerow(["The currency of the United States is the US Dollar."])
    csv_writer.writerow(["The US has a rich history with a significant impact on global politics and economy."])

# Using with statement to append to an existing file
with open('usa_new.csv', 'a', newline='') as file:
    csv_writer = csv.writer(file)
    csv_writer.writerow(["The Statue of Liberty is one of the most iconic landmarks in the US."])
    csv_writer.writerow(["The United States has some of the best universities in the world such as MIT and Harvard."])

In this tutorial, I have explained to you, how to write strings to a .csv file in Python. We have learned the process of creating a new CSV file and appending data to an existing one. By utilizing Python’s built-in csv module, we have seen how simple and efficient it can be to handle CSV files.

Other Python tutorials you may like: