[CSV (Comma Separated Values)]即逗号分隔值(也称字符分隔值,因为分隔符可以不是逗号),是一种常用的文本格式,用以存储表格数据,包括数字或者字符。
1. 利用文件类型直接迭代访问(含有中文)
with open('testdata.csv', encoding="GB2312") as f:
for line in f:
row = line.split(',')
2. 利用csv.reader()迭代访问
with open('testdata.csv', encoding="GB2312") as f:
reader = csv.reader(f)
for row in reader:
print(row)
3.跳过首行标题行
with open('testdata.csv', encoding="GB2312") as f:
reader = csv.reader(f)
#跳过首行
header = next(reader)
for row in reader:
print(row)
4. 跳过符合条件的行
with open('testdata.csv', encoding="GB2312") as f:
reader = csv.reader(f)
for row in reader:
#利用正则匹配跳过第一列为'file'
if re.match(r"^file$",rows[0]):
pass
else:
print(row)
5. 跳过第n行
with open('testdata.csv', encoding="GB2312") as f:
reader = csv.reader(f)
for i, row in enumerate(reader):
if i == 5:
pass
else:
print(row)
6打印行号
with open('testdata.csv', encoding="GB2312") as f:
reader = csv.reader(f)
for i,rows in enumerate(reader):
print("ROW #{0} {1}".format(i,rows))
7操作csv记录list,可排序,可转化字符串
with open('testdata.csv', encoding="GB2312") as f:
reader = csv.reader(f)
header = next(reader)
for rows in reader:
rows.sort()
print("|".join(rows))
8利用csv.reader读取键值对csv, 存入dict
with open("instance.csv") as csvFile:
mDict = {}
reader = csv.DictReader(csvFile)
for row in reader:
#跳过标题行
if reader.line_num == 1:
continue
#生成字典
mDict[row[0]]=row[1]
print(mDict)
8利用csv.Dictreader读取键值对csv, 存入dict
with open("instance.csv") as csvFile:
result = {}
reader = csv.DictReader(csvFile)
for row in reader:
result[row["name"]]=row["score"]
print(result)
网友评论