python-20-如何读写CSV数据?

这里写图片描述

我们通过csv库reader函数和writer函数来执行读和写操作。这两个函数都需要传入一个文件对象
这里写图片描述
这里写图片描述

python3和python2的写法有点不一样
这里写图片描述

报错:_csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)

Sorry, folks, we've got an understanding problem here. CSV files are
typically NOT created by text editors. They are created e.g. by "save as
csv" from a spreadsheet program, or as an output option by some database
query program. They can have just about any character in a field,
including \r and \n. Fields containing those characters should be quoted
(just like a comma) by the csv file producer. A csv reader should be
capable of reproducing the original field division. Here for example is
a dump of a little file I just created using Excel 2003:
...
This sentence in the documentation is NOT an error: """If csvfile is a
file object, it must be opened with the ‘b’ flag on platforms where that
makes a difference."""

python 3.5: TypeError: a bytes-like object is required, not ‘str’
出现该错误往往是通过open()函数打开文本文件时,使用了‘rb’属性,如:fileHandle=open(filename,’rb’),则此时是通过二进制方式打开文件的,所以在后面处理时如果使用了str()函数,就会出现该错误,该错误不会再python2中出现。
在open()函数中使用‘r’属性,即文本方式读取,而不是‘rb’,以二进制文件方式读取,可以直接解决问题。

import urllib.request

import csv

urllib.request.urlretrieve('http://table.finance.yahoo.com/table.csv?s=000001.sz','pingan.csv')

rf = open('pingan.csv','r')

reader = csv.reader(rf)

print(next(reader))
print('================================')
print(next(reader))
print('================================')
print(next(reader))
print('================================')

写入另一个文件:

wf = open('pingan_copy.csv','w')
writer = csv.writer(wf)
writer.writerow(['Date', 'Open', 'High', 'Low', 'Close', 'Volume', 'Adj Close'])
writer.writerow(next(reader))
wf.flush()

问题解决:

with open('pingan.csv','r') as rf:
    reader = csv.reader(rf)
    with open('pingan2.csv' ,'w') as wf:
        writer = csv.writer(wf)
        headers = next(reader)
        writer.writerow(headers)
        for row in reader:
            if row[0] < '2016-01-01':
                break
            if int(row[5]) >= int(50000000):
                writer.writerow(row)

print('end')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值