[英]Python csv.DictReader does not split at comma's
A row in my csv file: 我的csv文件中的一行:
console_ip,spsstatus,spsversion
console_ip,spsstatus,spsversion
172.19.126.182,Unable to send RAW command (channel=0x6 netfn=0x6 lun=0x0 cmd=0x4 rsp=0x83): Unknown (0x83)^M,Unable to send RAW command (channel=0x6 netfn=0x6 lun=0x0 cmd=0x1 rsp=0x83): Unknown (0x83)
172.19.126.182,无法发送RAW命令(通道= 0x6 netfn = 0x6 lun = 0x0 cmd = 0x4 rsp = 0x83):未知(0x83)^ M,无法发送RAW命令(通道= 0x6 netfn = 0x6 lun = 0x0 cmd = 0x1 rsp = 0x83):未知(0x83)
I am getting: 我正进入(状态:
{'console_ip': '172.19.126.182', 'spsversion': None, 'spsstatus': 'Unable to send RAW command (channel=0x6 netfn=0x6 lun=0x0 cmd=0x4 rsp=0x83): Unknown (0x83)'} {'console_ip': '', 'spsversion': None, 'spsstatus': 'Unable to send RAW command (channel=0x6 netfn=0x6 lun=0x0 cmd=0x1 rsp=0x83): Unknown (0x83)'}
{'console_ip':'172.19.126.182','spsversion':无,'spsstatus':'无法发送RAW命令(channel = 0x6 netfn = 0x6 lun = 0x0 cmd = 0x4 rsp = 0x83):未知(0x83)' } {'console_ip':``,'spsversion':无,'spsstatus':'无法发送RAW命令(channel = 0x6 netfn = 0x6 lun = 0x0 cmd = 0x1 rsp = 0x83):未知(0x83)'}
Expected just one element in a dictionary: 预期字典中只有一个元素:
{'console_ip': '172.19.126.182', 'spsversion': 'Unable to send RAW command (channel=0x6 netfn=0x6 lun=0x0 cmd=0x4 rsp=0x83): Unknown (0x83)','spsstatus': 'Unable to send RAW command (channel=0x6 netfn=0x6 lun=0x0 cmd=0x1 rsp=0x83): Unknown (0x83)'}
{'console_ip':'172.19.126.182','spsversion':'无法发送RAW命令(channel = 0x6 netfn = 0x6 lun = 0x0 cmd = 0x4 rsp = 0x83):未知(0x83)','spsstatus':'无法发送RAW命令(通道= 0x6 netfn = 0x6 lun = 0x0 cmd = 0x1 rsp = 0x83):未知(0x83)'}
My code: 我的代码:
f = 'consolespsdata/hostIpmi_bom52'
with open(f, 'rU') as csv_file:
csv_reader = csv.DictReader(csv_file,delimiter=",")
for line in csv_reader:
if line['console_ip'] is not None:
print line
Im wondering what might be causing this issue. 我想知道是什么引起了这个问题。
a.csv a.csv
console_ip,spsstatus,spsversion
172.19.126.182,Unable to send RAW command (channel=0x6 netfn=0x6 lun=0x0 cmd=0x4 rsp=0x83): Unknown (0x83)^M,Unable to send RAW command (channel=0x6 netfn=0x6 lun=0x0 cmd=0x1 rsp=0x83): Unknown (0x83)
b.py b.py
import csv
f = 'a.csv'
with open(f, 'rU') as csv_file:
csv_reader = csv.DictReader(csv_file,delimiter=",")
for line in csv_reader:
if line['console_ip'] is not None:
print line
python b.py python b.py
{'console_ip': '172.19.126.182',
'spsversion': 'Unable to send RAW command (channel=0x6 netfn=0x6 lun=0x0 cmd=0x1 rsp=0x83): Unknown (0x83)',
'spsstatus': 'Unable to send RAW command (channel=0x6 netfn=0x6 lun=0x0 cmd=0x4 rsp=0x83): Unknown (0x83)^M'
}
a.csv a.csv
k1,k2,k3
val1,val2,val3
val4,val5,val6
b.py b.py
import csv
f = 'a.csv'
with open(f, 'rU') as csv_file:
csv_reader = csv.DictReader(csv_file,delimiter=",")
for line in csv_reader:
if line['k1'] is not None:
print line
python b.py python b.py
{'k3': 'val3', 'k2': 'val2', 'k1': 'val1'}
{'k3': 'val6', 'k2': 'val5', 'k1': 'val4'}
Which seems to work fine. 这似乎工作正常。 If the second example works for you, maybe special characters in your
csv
files are causing issues. 如果第二个示例适合您,则可能是
csv
文件中的特殊字符引起了问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.