天天看點

python-20-如何讀寫CSV資料?

python-20-如何讀寫CSV資料?

我們通過csv庫reader函數和writer函數來執行讀和寫操作。這兩個函數都需要傳入一個檔案對象

python-20-如何讀寫CSV資料?
python-20-如何讀寫CSV資料?

python3和python2的寫法有點不一樣

python-20-如何讀寫CSV資料?

報錯:_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 :
...
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[] < '2016-01-01':
                break
            if int(row[]) >= int():
                writer.writerow(row)

print('end')