Python - 字典列表作为字符串写入 CSV,而不是列表 - 稍后无法解析为 pandas

发布于 2025-01-12 08:33:21 字数 1428 浏览 5 评论 0原文


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 时 - 它显示为不带引号的列表:
Discovered_host 输出

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:
Discovered_host output

The CSV shows on the 'offboarded_hosts' file, quotes on the string on multiple entries - but not single list items:
Offboarded Host output

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凶凌 2025-01-19 08:33:21

建议:是否可以评估每一行,如果其长度小于 2,则可以从中构建一个列表?

Suggestion: Is it possible to evaluate each line and if its length is <2 you build a list out of it?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文