python2csv文件操作,python读写csv文件(csv)

[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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值