How To Upload A Csv File Into Tkinter
Summary: in this tutorial, y'all'll learn how to write information into a CSV file using the born csv
module.
Steps for writing a CSV file
To write data into a CSV file, you follow these steps:
- Kickoff, open the CSV file for writing (
due west
mode) by using theopen()
function. - Second, create a CSV author object by calling the
writer()
part of thecsv
module. - Third, write data to CSV file past calling the
writerow()
orwriterows()
method of the CSV writer object. - Finally, close the file once you consummate writing data to it.
The following code illustrates the above steps:
import csv # open up the file in the write mode f = open('path/to/csv_file', 'w') # create the csv writer author = 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 apply the with
statement so that y'all don't need to call the close()
method to explicitly close the file:
import csv # open the file in the write fashion with open('path/to/csv_file', 'due west') as f: # create the csv author 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 demand to specify the graphic symbol 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', 'westward', encoding='UTF8') equally f: # create the csv writer author = csv.author(f) # write a row to the csv file writer.writerow(row)
Code language: PHP ( php )
Writing to CSV files instance
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', 'westward', encoding='UTF8') equally f: writer = csv.writer(f) # write the header writer.writerow(header) # write the data writer.writerow(information)
Code language: PHP ( php )
If you open the countries.csv
, yous'll see one issue that the file contents have an additional bare line betwixt two subsequent rows:
To remove the blank line, you pass the keyword argument newline=''
to the open()
part every bit 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: author = csv.author(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 lot use the writerows()
method of the CSV author 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, 'Equally', 'ASM'], ['Andorra', 468, 'AD', 'AND'], ['Angola', 1246700, 'AO', 'Agone'] ] with open up('countries.csv', 'w', encoding='UTF8', newline='') as f: writer = csv.writer(f) # write the header writer.writerow(header) # write multiple rows writer.writerows(data)
Lawmaking language: PHP ( php )
Writing to CSV files using the DictWriter course
If each row of the CSV file is a lexicon, yous can use the DictWriter
class of the csv
module to write the dictionary to the CSV file.
The example illustrates how to employ the DictWriter course to write data to a CSV file:
import csv # csv header fieldnames = ['name', 'expanse', 'country_code2', 'country_code3'] # csv data rows = [ {'name': 'Republic of albania', 'expanse': 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() author.writerows(rows)
Code language: PHP ( php )
How it works.
- Commencement, define variables that hold the field names and information rows of the CSV file.
- Next, open the CSV file for writing by calling the
open()
part. - Then, create a new instance of the
DictWriter
class past passing the file object (f
) andfieldnames
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 Author or the
DictWriter
form to write data to a CSV file.
Did you find this tutorial helpful ?
Source: https://www.pythontutorial.net/python-basics/python-write-csv-file/
Posted by: andersonobecam.blogspot.com
0 Response to "How To Upload A Csv File Into Tkinter"
Post a Comment