Python - 字典列表作为字符串写入 CSV,而不是列表 - 稍后无法解析为 pandas
for k,v in groupDict_sanitized.items():
if discovered_hosts_groupid in v:
csvdata = (k,v)
print(k,v)
print(type(v))
#Example from print output
#20099 ['5']
#<class 'list'>
#20100 ['5']
#<class 'list'>
#20105 ['5']
#<class 'list'>
#20114 ['5']
#<class 'list'>
write_discovered_hosts_raw_csv.writerows([csvdata])
if offboarded_groupid in v:
#v = list(v)
csvdata = (k,v)
print(k,v)
print(type(v))
#Example from print output:
#19621 ['192', '359']
#<class 'list'>
#19636 ['19', '23', '56', '192']
#<class 'list'>
#19657 ['19', '21', '64', '192', '408']
#<class 'list'>
#19667 ['192']
write_offboarded_hosts_raw_csv.writerows([csvdata])
照片中显示的 Discovered_host CSV 输出在列表周围没有“”(尽管只有一个列表条目)是一个 class 。写入 csvdata 时 - 它显示为不带引号的列表:
CSV 在“offboarded_hosts”文件中显示多个条目上字符串的引号 - 但不是单个列表项:
中重新读取此内容时,由于“[list]”条目,它显示为空数据框,其中不带引号的 CSV 确实读取为正确的数据框。
我在编写 CSV 列表时是否缺少一条规则,将其放置为非引用列表,但保留所需的格式?
for k,v in groupDict_sanitized.items():
if discovered_hosts_groupid in v:
csvdata = (k,v)
print(k,v)
print(type(v))
#Example from print output
#20099 ['5']
#<class 'list'>
#20100 ['5']
#<class 'list'>
#20105 ['5']
#<class 'list'>
#20114 ['5']
#<class 'list'>
write_discovered_hosts_raw_csv.writerows([csvdata])
if offboarded_groupid in v:
#v = list(v)
csvdata = (k,v)
print(k,v)
print(type(v))
#Example from print output:
#19621 ['192', '359']
#<class 'list'>
#19636 ['19', '23', '56', '192']
#<class 'list'>
#19657 ['19', '21', '64', '192', '408']
#<class 'list'>
#19667 ['192']
write_offboarded_hosts_raw_csv.writerows([csvdata])
The Discovered_host CSV output, shown in the photo does not have "" around the list(although only a single list entry) is a class . When writing csvdata - it shows as a list without quotes:
The CSV shows on the 'offboarded_hosts' file, quotes on the string on multiple entries - but not single list items:
The issue here is, when re-reading this later in different code, it shows as an empty data frame due to the "[list]" entry where the CSV without quotes does read as a proper data frame.
Is there a rule I'm missing on CSV list writing to place this as a non-quoted list but keeping this desired format?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
建议:是否可以评估每一行,如果其长度小于 2,则可以从中构建一个列表?
Suggestion: Is it possible to evaluate each line and if its length is <2 you build a list out of it?