python - Python 与 R 的 fromJSON()、unlist()、attr() 等价的函数?
问题描述
我用 R 写了一个代码,但它不能处理我的大文件,所以我改用 Python。但我一直在寻找 3 个 Python 的 R 等效函数,但没有找到任何令人满意的结果。
3 R 函数是:
fromJSON()
unlist()
attr()
好的,让我向您展示我的问题,向您展示我在 R 中所做的事情和得到的结果。现在我想在 Python 中做同样的事情。
json_file <- '[
{
"id": "haha",
"type": "table",
"A": "HKD",
"B": "HKD",
"C": "HKD",
"V": "HKD",
"composition": [
{
"id": "AO",
"type": "panier"
},
{
"id": "KK",
"type": "basket",
"isAutoDiv": false,
"composition": [
{
"id": "600",
"type": "apple",
"number": 1.11
},
{
"id": "605",
"type": "peach",
"number": 1.79
}
]
},
{
"id": "KL",
"type": "basket"
}
]
},
{
"id": "hoho",
"type": "table",
"composition": [
{
"id": "KT",
"type": "panier"
},
{
"id": "OT",
"type": "panier"
},
{
"id": "CL",
"type": "basket",
"isAutoDiv": false,
"composition": [
{
"id": "450",
"type": "apple"
},
{
"id": "630",
"type": "orange"
},
{
"id": "023",
"type": "orange",
"composition": [
{
"id": "AOOOOOOO",
"type": "orangejuice"
},
{
"id": "VMVMVMVMV",
"type": "orangejuice"
}
]
}
]
}
]
}
]'
现在我这样做:1:
nestedjson <- fromJSON(json_file)
我可以得到:
[[1]]
[[1]]$id
[1] "haha"
[[1]]$type
[1] "table"
[[1]]$A
[1] "HKD"
[[1]]$B
[1] "HKD"
[[1]]$C
[1] "HKD"
[[1]]$V
[1] "HKD"
[[1]]$composition
[[1]]$composition[[1]]
[[1]]$composition[[1]]$id
[1] "AO"
[[1]]$composition[[1]]$type
[1] "panier"
[[1]]$composition[[2]]
[[1]]$composition[[2]]$id
[1] "KK"
[[1]]$composition[[2]]$type
[1] "basket"
[[1]]$composition[[2]]$isAutoDiv
[1] FALSE
[[1]]$composition[[2]]$composition
[[1]]$composition[[2]]$composition[[1]]
[[1]]$composition[[2]]$composition[[1]]$id
[1] "600"
[[1]]$composition[[2]]$composition[[1]]$type
[1] "apple"
[[1]]$composition[[2]]$composition[[1]]$number
[1] 1.11
[[1]]$composition[[2]]$composition[[2]]
[[1]]$composition[[2]]$composition[[2]]$id
[1] "605"
[[1]]$composition[[2]]$composition[[2]]$type
[1] "peach"
[[1]]$composition[[2]]$composition[[2]]$number
[1] 1.79
[[1]]$composition[[3]]
[[1]]$composition[[3]]$id
[1] "KL"
[[1]]$composition[[3]]$type
[1] "basket"
[[2]]
[[2]]$id
[1] "hoho"
[[2]]$type
[1] "table"
[[2]]$composition
[[2]]$composition[[1]]
[[2]]$composition[[1]]$id
[1] "KT"
[[2]]$composition[[1]]$type
[1] "panier"
[[2]]$composition[[2]]
[[2]]$composition[[2]]$id
[1] "OT"
[[2]]$composition[[2]]$type
[1] "panier"
[[2]]$composition[[3]]
[[2]]$composition[[3]]$id
[1] "CL"
[[2]]$composition[[3]]$type
[1] "basket"
[[2]]$composition[[3]]$isAutoDiv
[1] FALSE
[[2]]$composition[[3]]$composition
[[2]]$composition[[3]]$composition[[1]]
[[2]]$composition[[3]]$composition[[1]]$id
[1] "450"
[[2]]$composition[[3]]$composition[[1]]$type
[1] "apple"
[[2]]$composition[[3]]$composition[[2]]
[[2]]$composition[[3]]$composition[[2]]$id
[1] "630"
[[2]]$composition[[3]]$composition[[2]]$type
[1] "orange"
[[2]]$composition[[3]]$composition[[3]]
[[2]]$composition[[3]]$composition[[3]]$id
[1] "023"
[[2]]$composition[[3]]$composition[[3]]$type
[1] "orange"
[[2]]$composition[[3]]$composition[[3]]$composition
[[2]]$composition[[3]]$composition[[3]]$composition[[1]]
[[2]]$composition[[3]]$composition[[3]]$composition[[1]]$id
[1] "AOOOOOOO"
[[2]]$composition[[3]]$composition[[3]]$composition[[1]]$type
[1] "orangejuice"
[[2]]$composition[[3]]$composition[[3]]$composition[[2]]
[[2]]$composition[[3]]$composition[[3]]$composition[[2]]$id
[1] "VMVMVMVMV"
[[2]]$composition[[3]]$composition[[3]]$composition[[2]]$type
[1] "orangejuice"
然后我这样做2:
unnestedjson <- unlist(nestedjson)
我可以得到:
id
"haha"
type
"table"
A
"HKD"
B
"HKD"
C
"HKD"
V
"HKD"
composition.id
"AO"
composition.type
"panier"
composition.id
"KK"
composition.type
"basket"
composition.isAutoDiv
"FALSE"
composition.composition.id
"600"
composition.composition.type
"apple"
composition.composition.number
"1.11"
composition.composition.id
"605"
composition.composition.type
"peach"
composition.composition.number
"1.79"
composition.id
"KL"
composition.type
"basket"
id
"hoho"
type
"table"
composition.id
"KT"
composition.type
"panier"
composition.id
"OT"
composition.type
"panier"
composition.id
"CL"
composition.type
"basket"
composition.isAutoDiv
"FALSE"
composition.composition.id
"450"
composition.composition.type
"apple"
composition.composition.id
"630"
composition.composition.type
"orange"
composition.composition.id
"023"
composition.composition.type
"orange"
composition.composition.composition.id
"AOOOOOOO"
composition.composition.composition.type
"orangejuice"
composition.composition.composition.id
"VMVMVMVMV"
composition.composition.composition.type
"orangejuice"
最后我这样做了3:
unnestednames <- attr(unnestedjson, "names")
我可以得到以下结果: 这就是我想要的 Python 代码的全部内容:名称列表,其中包含“组合”的属性名称。可以显示属性在哪个级别。
例如,第 2 层嵌套的属性名称为“composition.type”,第 4 层嵌套的属性名称为“composition.composition.composition.id”。
[1] "id"
[2] "type"
[3] "A"
[4] "B"
[5] "C"
[6] "V"
[7] "composition.id"
[8] "composition.type"
[9] "composition.id"
[10] "composition.type"
[11] "composition.isAutoDiv"
[12] "composition.composition.id"
[13] "composition.composition.type"
[14] "composition.composition.number"
[15] "composition.composition.id"
[16] "composition.composition.type"
[17] "composition.composition.number"
[18] "composition.id"
[19] "composition.type"
[20] "id"
[21] "type"
[22] "composition.id"
[23] "composition.type"
[24] "composition.id"
[25] "composition.type"
[26] "composition.id"
[27] "composition.type"
[28] "composition.isAutoDiv"
[29] "composition.composition.id"
[30] "composition.composition.type"
[31] "composition.composition.id"
[32] "composition.composition.type"
[33] "composition.composition.id"
[34] "composition.composition.type"
[35] "composition.composition.composition.id"
[36] "composition.composition.composition.type"
[37] "composition.composition.composition.id"
[38] "composition.composition.composition.type"
我一直在寻找一些可以做同样事情但没有找到任何东西的 Python 函数。如果您有任何想法在 Python 中执行这些操作,那将解决我的大问题!
非常感谢!
解决方案
我的回答是偏颇的。我只解释了如何取消列出。
对于我们未列出的数字,如下所示:
不列出数字
例如:
aa = [1,2,3,4]
for i in aa:
print(i, end="" )
对于字符串,我们需要像 bleow 一样取消列出:
c=["a", "ja", "ra", "ya", "ma"]
ab= ",". join(c)
print(ab)
使用 Pd.series
ex 取消列出的其他方法:
vector = [1,2,3,4,5]
print(pd.Series(vector))
再次想转换成列表:
square_vector = pd.Series(vector)
print(square_vector.tolist())
推荐阅读
- android - 如何实现 Google Play 促销代码兑换?
- oracle - 在 Oracle 中删除对象时保留特权
- javascript - Javascript测试对象中是否存在对象键,其数组使用值而不是索引
- tfsbuild - TFS REST API:将构建结果从部分部分成功更改为成功
- node.js - 在arm上安装canvas-prebuilt时出现nodejs错误
- javascript - Vue.js object proxies: props not showing in Object.keys
- node.js - 调试节点上丢失的 HTTPS UPGRADE 请求
- image-processing - 如何使用 opencv-python 从信用卡图像中提取详细信息?
- javascript - 反应路由器链接右键单击不起作用
- chromium - 将第三方库添加到 chromium