January 2019
Beginner
318 pages
8h 23m
English
To write data in a csv file, we use the csv.writer module. In this section, we will store some data into the Python list and then put that data into the csv file. Create a script called csv_write.py and write the following content in it:
import csvwrite_csv = [['Name', 'Sport'], ['Andres Iniesta', 'Football'], ['AB de Villiers', 'Cricket'], ['Virat Kohli', 'Cricket'], ['Lionel Messi', 'Football']]with open('csv_write.csv', 'w') as csvFile: writer = csv.writer(csvFile) writer.writerows(write_csv) print(write_csv)
Run the script and you will get the following output:
student@ubuntu:~$ python3 csv_write.py
Following is the output:
[['Name', 'Sport'], ['Andres Iniesta', 'Football'], ['AB de Villiers', 'Cricket'], ['Virat ...
Read now
Unlock full access