1, examples
1, this is a csv file I manually created content is copy and paste from Word to table
2, which I used to read data code
import csv
file = open(r"C:\Users\Administrator\Desktop\cs1.csv","r",errors="ignore",encoding="utf-8")
csv_file = csv.reader(file)
for i in csv_file:
print(i)
file.close()
读取的结果:
['ID\t\tӢģ\t\tCODE']
['SP91FD79GFQU\t\tAndorra la Vella\t\tAD']
['THM3PVJHX1G2\tAr Ruways\tAr Ruways\t\tAE ']
There graph results can be seen, not only did not play out the results of the Chinese, and each column of the result are behind more than a "\ t"
3, on the Internet to find some information found that it is no problem, then think again created a data and manually input data (table named cs.csv, the data is the same)
Code and results are as follows:
import csv
file = open(r"C:\Users\Administrator\Desktop\cs.csv","r",errors="ignore")
csv_file = csv.reader(file)
for i in csv_file:
print(i)
file.close()
结果:
['城市ID', '城市名', '城市名(英文)', '国家', '国家CODE ']
['SP91FD79GFQU', '安道尔', 'Andorra la Vella', '安道尔', 'AD ']
['THM3PVJHX1G2', 'Ar Ruways', 'Ar Ruways', '阿联酋', 'AE ']
And the above comparison can be found, the last csv data is manually re-created, "\ t" will be gone, probably Word copy and paste into the problems caused by; then Chinese show, canceled the encoding = "utf-8" after the results only show the normal Chinese, see the online presentation also need to add to this, the reason is not clear
2, csv some of the common practices
1, using the reader function, receiving the object (such as csv file) an iterative, can return a generator, which can be parsed from the contents of the csv following code can read the entire contents of the csv, in units
import csv
path = r"C:\Users\Administrator\Desktop\cs.csv"
file = open(path,"r",errors="ignore")
csv_file = csv.reader(file)
for i in csv_file:
print(i)
file.close()
结果:
['城市ID', '城市名', '城市名(英文)', '国家', '国家CODE ']
['SP91FD79GFQU', '安道尔', 'Andorra la Vella', '安道尔', 'AD ']
['THM3PVJHX1G2', 'Ar Ruways', 'Ar Ruways', '阿联酋', 'AE ']
2, wherein a column method may be used to extract the following
import csv
path = r"C:\Users\Administrator\Desktop\cs.csv"
file = open(path,errors="ignore")
csv_file = csv.reader(file)
column = [row[0] for row in csv_file]
print(column)
file.close()
结果:
['城市ID', 'SP91FD79GFQU', 'THM3PVJHX1G2']
How many lines 3, before reading
path = r"C:\Users\Administrator\Desktop\cs.csv"
eFile = open(path)
#读取csv文件
eReader=csv.reader(eFile)
#遍历csv对象获取数据,每一条数据都是一个list,每一列是list中的一个元素
#line_num是行号,这里只读取前100行
for row in eReader:
if eReader.line_num <= 1:
print('行 '+str(eReader.line_num) + ': '+str(row)) #打印行号
else:
break
#关闭文件
eFile.close()
结果:
行 1: ['城市ID', '城市名', '城市名(英文)', '国家', '国家CODE ']
4, write csv file from the list, set the newline, otherwise it will be a blank line between two lines
import csv
l1 = ["1","2","3"]
path = r"C:\Users\Administrator\Desktop\cs3.csv"
file = open(path,'w',newline='')
writer = csv.writer(file)
m = len(l1)
for i in range(m):
writer.writerow(l1[i])
file.close()
结果:
5, using multiple lines written list
import csv
l1 = ['城市ID', '城市名', '城市名(英文)', '国家', '国家CODE ']
l2 = ['SP91FD79GFQU', '安道尔', 'Andorra la Vella', '安道尔', 'AD ']
l3 = ['THM3PVJHX1G2', 'Ar Ruways', 'Ar Ruways', '阿联酋', 'AE ']
path = r"C:\Users\Administrator\Desktop\cs4.csv"
with open(path, 'w',newline='') as csvFile:
writer = csv.writer(csvFile)
# 写入多行用writerows
writer.writerows([l1, l2, l3])
结果:
6, write csv files from the dictionary
import csv
path = r"C:\Users\Administrator\Desktop\cs5.csv"
dic = {'张三': 123, '李四': 456, '王二娃': 789}
csvFile3 = open(path, 'w', newline='')
writer2 = csv.writer(csvFile3)
for key in dic:
writer2.writerow([key,dic[key]])
csvFile3.close()
结果: