python使用csv模块读写csv文件

╰半橙微兮° 2022-09-21 14:25 376阅读 0赞

原文:https://www.getdatajoy.com/examples/python-data-analysis/read-and-write-a-csv-file-with-the-csv-module

Example that shows how to read and write a Comma Separated Value (cvs) filewith thecsv python module.

This example reads a file named smallsample.cvs whose column labels arefirst_name,last_name, company_name, address, city,county,postal, phone1, phone2,email, web. First the csv module isimported.

  1. import csv

Now a new dialect called mydialect is created, the default dialect is excel

  1. csv.register_dialect(
  2. 'mydialect',
  3. delimiter = ',',
  4. quotechar = '"',
  5. doublequote = True,
  6. skipinitialspace = True,
  7. lineterminator = '\r\n',
  8. quoting = csv.QUOTE_MINIMAL)

Now we open and print the file and create an iterable object with thereader() routine, then we use afor loop to print the data.The with statement automatically closes the file when finished.

  1. print('\n Output from an iterable object created from the csv file')
  2. with open('smallsample.csv', 'rb') as mycsvfile:
  3. thedata = csv.reader(mycsvfile, dialect='mydialect')
  4. for row in thedata:
  5. print(row[0]+"\t \t"+row[1]+"\t \t"+row[4])

Output:

  1. Output from an iterable object created from the csv file
  2. first_name last_name city
  3. Aleshia Tomkiewicz St. Stephens Ward
  4. Evan Zigomalas Abbey Ward
  5. France Andrade East Southbourne and Tuckton W
  6. Ulysses Mcwalters Hawerby cum Beesby
  7. Tyisha Veness Greets Green and Lyng Ward
  8. Eric Rampy Desborough
  9. Marg Grasmick Bargate Ward
  10. Laquita Hisaw Chirton Ward
  11. Lura Manzella Staple Hill Ward

Dictionary From CSV File

This example opens the file and makes a dictionary whose keys are theentries in the first row of the data file. TheDictReader() routineaccomplished this.The with statement automatically closes the file when finished

  1. print("\n Now the output from a dictionary created from the csv file")
  2. with open('smallsample.csv', 'rb') as mycsvfile:
  3. dictofdata = csv.DictReader(mycsvfile, dialect='mydialect')
  4. for row in dictofdata:
  5. print(row['first_name']+"\t "+row['phone1']+"\t "+row['city'])

Output:

  1. Now the output from a dictionary created from the csv file
  2. Aleshia 01835-703597 St. Stephens Ward
  3. Evan 01937-864715 Abbey Ward
  4. France 01347-368222 East Southbourne and Tuckton W
  5. Ulysses 01912-771311 Hawerby cum Beesby
  6. Tyisha 01547-429341 Greets Green and Lyng Ward
  7. Eric 01969-886290 Desborough
  8. Marg 01865-582516 Bargate Ward
  9. Laquita 01746-394243 Chirton Ward
  10. Lura 01907-538509 Staple Hill Ward

Write An Array To A CSV File

Now an example about writing, a file called mydata.csv with the contents ofthe arrayarrayofdata will be created. The file is created by thewriter() routine, then each row is written withwriterow().The with statement automatically closes the file when finished

  1. arrayofdata=[[1,2,4,5,'something','spam',2.334],
  2. [3,1,6,3,'anything','spam',0]]
  3. with open('mydata.csv', 'w') as mycsvfile:
  4. thedatawriter = csv.writer(mycsvfile, dialect='mydialect')
  5. for row in arrayofdata:
  6. thedatawriter.writerow(row)

发表评论

表情:
评论列表 (有 0 条评论,376人围观)

还没有评论,来说两句吧...

相关阅读