繁体   English   中英

如何从我的 json 列中删除分隔的 pipe 并将它们拆分为不同的列及其各自的值

[英]How to remove delimeted pipe from my json column and split them to different columns and their respective values

“描述”:ID|100|\n姓名|Sam|\n城市|纽约市|\n州|纽约|\n联系方式|1234567890|\n电子邮件|1234@yahoo.com|

这就是我在 json 中的代码的样子。 我想将此 json 文件转换为 excel 表以将嵌套列拆分为单独的列,并已使用 pandas 来实现它,但无法实现。 我想要在 excel 表中的 output 是:

ID 名称 城市 State 联系人 Email 100 Sam New York City New York 1234567890 1234@yahoo.com

我想移除这些管道,解决方案应该在 pandas 中。 这个你能帮我吗。

这可能是一种方式:

import pandas as pd

j = {"description": "ID|100|\nName|Sam|\nCity|New York City|\nState|New York|\nContact|1234567890|\nEmail|1234@yahoo.com|"}

desc = j.get("description")
attributes = desc.split("\n")

df = pd.DataFrame()
d = {}

for attrib in attributes:
    (k, v, sp) = attrib.split("|")
    d[k] = v

df = df.append(d, ignore_index=True)
print(df)
df.to_csv("output.csv")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM