我有一个包含数据的文件,例如,
{"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/