程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Read and write CSV file details in Python

編輯:Python

Catalog

Preface

1. Import CSV library

2. Yes CSV Read and write files

2.1 Write... As a list CSV file

2.2 Read... In list form CSV file

2.3 Write... In dictionary form csv file

2.4 Read in dictionary form csv file

Conclusion

Preface

CSV(Comma-Separated Values) Comma separated values , A text file separated by commas and stored in rows , All values are expressed as string types ( Be careful : The number is of string type ). If CSV There are Chinese , Should use utf-8 Coded reading and writing .

1. Import CSV library

python Chinese vs csv The file has its own library, which can be used , When we want to csv When reading and writing files, you can import them directly .

import csv2. Yes CSV Read and write files 2.1 Write... As a list CSV file

grammar :csv.writer(f):
writer Support writerow( list ) Single line write , and writerows( Nested list ) Batch write multiple lines , No need to save it manually .

import csvheader = ['name','age','QQ_num','wechat']data = [['suliang','21','787991021','lxzy787991021']]with open ('information.csv','w',encoding='utf-8',newline='') as fp: # Write writer =csv.writer(fp) # Set the first row header writer.writerow(header) # Write data to writer.writerows(data)

result :

Be careful : When opening a file, you should specify a format of w, Text writing .
When opening a file , Specifies that new rows are not automatically added newline=‘’, Otherwise, one or more blank lines will be written for each line .

2.2 Read... In list form CSV file

grammar :csv.reader(f, delimiter=‘,’)
reader Generator , Read one line at a time , Each row of data is in list format , Can pass delimiter Parameter specifies the separator

import csvwith open('information.csv',encoding='utf-8')as fp: reader = csv.reader(fp) # Get the title header = next(reader) print(header) # Traversal data for i in reader: print(i)

result :

Be sure to read and write files when necessary , When you are uncertain, you can write an absolute path . To get csv The content needs to be traversed and then output .

2.3 Write... In dictionary form csv file

grammar :csv.DicWriter(f):
When writing, you can use writeheader() Write the title , And then use writerow( Dictionary format data row ) or writerows( Multi row data )

import csvheader = ['name','age']data = [{'name':'suliang','age':'21'}, {'name':'xiaoming','age':'22'}, {'name':'xiaohu','age':'25'}]with open ('information.csv','w',encoding='utf-8',newline='') as fp: # Write writer =csv.DictWriter(fp,header) # Write the title writer.writeheader() # Write data to writer.writerows(data)

result :

2.4 Read in dictionary form csv file

grammar :csv.DicReader(f, delimiter=‘,’)
Directly assemble the title and each column of data into an ordered Dictionary (OrderedDict) Format , There is no need to read the header line separately

import csvwith open('information.csv',encoding='utf-8')as fp: reader = csv.DictReader(fp) for i in reader print(i)

result :

attach :csv Reading and writing mode

r: Open the file read , Can read file information

w: File opened in write mode , Information can be written to the file . If the file exists , Leave blank , Write again

a: Open file in append mode , When opening a file, the pointer can be moved to the end , Create if file does not exist

r+: Open the file read-write , Can read and write files

w+: Eliminate document content , Open the file read-write

a+: Open the file read-write , The file pointer moves to the end

b: Open the file in binary

Conclusion

This article is about in python Read and write to CSV That's all for the details of the document , More about python Reading and writing CSV Please search the previous articles of software development network or continue to browse the relevant articles below. I hope you will support software development network more in the future !



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved