[英]Incorrect quotes display while reading a pipe separated csv file with python read_csv()
我正在嘗試使用 python read_csv()
讀取管道分隔的 csv 文件,但它錯誤地檢測到第一個字段中的雙引號。 我知道它與 csv 閱讀器標志有關,但無法弄清楚。
代碼
with open('Find_File.dat', 'r') as csvfile:
myfile=csv.reader(csvfile,escapechar=" ' ",quoting=csv.QUOTE_NONE,quotechar="'",doublequote=True)
for row in myfile:
data.append(row)
print(data[0])
預期產出
[“TCC記錄標識符”|“用戶名”|“候選人ID”|“經驗”|“工作序列”|“當前10”|“工資10的結束率”]
實際輸出
['TCC 記錄標識符|“用戶名”|“候選人 ID”|“經驗”|“工作序列”|“當前 10”|“工資結束率 10” ']
您的預期輸出有點奇怪。 您不想將該行解析為列列表嗎? 此外,如果分隔符是管道,則未指定。
鑒於輸入文件如下所示:
"TCC Record Identifier"|"UserName"|"Candidate ID"|"Experience"|"Job Sequence"|"Current 10"|"Ending Rate of Pay 10"
甚至:
TCC Record Identifier|UserName|Candidate ID|Experience|Job Sequence|Current 10|Ending Rate of Pay 10
默認的csv.reader
將通過簡單地覆蓋分隔符來解析上述任一輸入:
import csv
with open('input.csv',newline='') as f:
r = csv.reader(f,delimiter='|')
for row in r:
print(row)
輸出:
['TCC Record Identifier', 'UserName', 'Candidate ID', 'Experience', 'Job Sequence', 'Current 10', 'Ending Rate of Pay 10']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.