我正在使用 CSV 閱讀器在 Python 中讀取 TSV。代碼是:
f = csv.reader(open('sample.csv'), delimiter='\t')
for chunk in f:
print(chunk)
選項卡分隔的 CSV 檔案中的一行如下所示(此處托管的 csv ):
| 檔案 | unit1_toks | unit2_toks | unit1_txt1 | unit2_txt2 | s1_toks | s2_toks | unit1_sent | unit2_sent | 目錄 |
|---|---|---|---|---|---|---|---|---|---|
| GUM_bio_galois | 156-160 | 161-170 | " 我們 zouden dan voorstellen | 導演作品 | 107-182 | 107-182 | 泊松宣布伽羅瓦的作業“不可理解”,并宣稱“[伽羅瓦']論證是不夠的。” [16] | Poisson 宣布 Galois 的作業“不可理解”,并宣稱“[Galois'] 論證將表明作者應該發表意見。” [16] | 1>2 |
我得到以下輸出(CSV 閱讀器缺少一些制表符空間):
['GUM_bio_galois',
'156-160',
'161-170',
' We zouden dan voorstellen\tdat de auteur al zijn werk zou moeten publiceren\t107-182\t107-182\tPoisson declared Galois \' work incomprehensible " , declaring that " [ Galois \' ] argument is not sufficient . " [ 16 ]',
'Poisson declared Galois \' work " incomprehensible " , declaring that " [ Galois \' ] argument would then suggest that the author should publish the opinion . " [ 16 ]',
'1>2']
我希望它看起來像這樣:
['GUM_bio_galois',
'156-160',
'161-170',
'" We zouden dan voorstellen',
'dat de auteur al zijn werk zou moeten publiceren',
'107-182',
'107-182',
'Poisson declared Galois \' work incomprehensible " , declaring that " [ Galois \' ] argument is not sufficient . " [ 16 ]',
'Poisson declared Galois \' work " incomprehensible " , declaring that " [ Galois \' ] argument would then suggest that the author should publish the opinion . " [ 16 ]',
'1>2']
How can I get the CSV reader to handle incomplete quotes and retain them in my output?
uj5u.com熱心網友回復:
import csv
with open('sample.csv') as f:
rdr = csv.reader(f, quoting=csv.QUOTE_NONE, delimiter='\t')
header = next(rdr)
for line in rdr:
print(line)
或使用csv.DictReader:
import csv
with open('sample.csv') as f:
rdr = csv.DictReader(f, quoting=csv.QUOTE_NONE, delimiter='\t')
for line in rdr:
print(line)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/360602.html
標籤:python csv double-quotes csvreader
下一篇:來自CSV檔案的日期時間處理
