[英]Reading in CSV Files with newline characters imbedded
我目前正在從csv文件中讀取數據,並將令牌及其定義輸入到字典中。 該代碼可以正常工作,直到它到達CSV文件中的數據如下所示的位置:
"Token000\nip address\ntesttestest"
到目前為止,這是我的代碼:
for line in f:
if "Token" in line and re.search("Token\d", line):
commaIndex = line.index(",", line.index("Token"))
csvDict[line[line.index("Token"): commaIndex]] = line[commaIndex + 1: line.index(",", commaIndex + 1)]
用這個:
import csv
data={}
with open('your_file.csv') as csv_file:
reader=csv.reader(csv_file, skipinitialspace=True, quotechar="'")
for row in reader:
data[row[0]]=row[1:]
print(data)
我建議您看一下csv模塊文檔
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.