python - 如何将字典格式的txt文件转换为python中的数据帧?

标签 python pandas file dictionary

我有一个包含数据的文件,例如,

{"cid": "ABCD", "text": "alphabets", "time": "1 week", "author": "xyz"}
{"cid": "EFGH", "text": "verb", "time": "2 week", "author": "aaa"}
{"cid": "IJKL", "text": "noun", "time": "3 days", "author": "nop"}

我希望读取这个文件并创建一个数据框,例如,
cid     text    time    author
ABCD    alpha   1week   xyz
EFGH    verb    2week   aaa
IJKL    noun    3days   nop

最佳答案

您可以尝试使用不同的分隔符将文件读取为 csv 并抓取第一列,然后应用 ast.literal_eval转换为实际字典并转换回数据帧:

import ast
output = pd.DataFrame(pd.read_csv('file.txt',sep='|',header=None).iloc[:,0]
         .apply(ast.literal_eval).tolist())
print(output)

    cid       text    time author
0  ABCD  alphabets  1 week    xyz
1  EFGH       verb  2 week    aaa
2  IJKL       noun  3 days    nop

工作示例:
file = """{"cid": "ABCD", "text": "alphabets", "time": "1 week", "author":"xyz"}
{"cid": "EFGH", "text": "verb", "time": "2 week", "author": "aaa"}
{"cid": "IJKL", "text": "noun", "time": "3 days", "author": "nop"}"""

import io #dont need for reading a file directly , just for example
import ast
print(pd.DataFrame(pd.read_csv(io.StringIO(file),sep='|',header=None).iloc[:,0]
             .apply(ast.literal_eval).tolist()))
    cid       text    time author
0  ABCD  alphabets  1 week    xyz
1  EFGH       verb  2 week    aaa
2  IJKL       noun  3 days    nop
​

关于python - 如何将字典格式的txt文件转换为python中的数据帧?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59687201/

相关文章:

python - 附加两个 csv 文件时如何修复 pandas concat

python - 在不同的文件中分组写入行

c - 字符串和文件

python - MCMC 使用 Python 的司仪对麦克斯韦曲线进行采样

Python pandas 交叉选择相当于 .loc 和切片

python - 最佳放置裁剪区域以捕获图像中的特征( Blob )的算法

python - 在python中组合来自两个不同文本文件的列

python - 删除06、07等数字前的0

python - 访问 Pandas 数据框时出现关键错误

python - 如果值= 1,则pandas数据框打印列