xin*_*xin 4 python dictionary file pandas
我有一个包含以下数据的文件,
{"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"}
Run Code Online (Sandbox Code Playgroud)
我希望读取这个文件并创建一个像这样的数据框,
cid text time author
ABCD alpha 1week xyz
EFGH verb 2week aaa
IJKL noun 3days nop
Run Code Online (Sandbox Code Playgroud)
您可以尝试使用不同的分隔符将文件读取为 csv 并抓取第一列,然后应用ast.literal_eval转换为实际字典并转换回数据帧:
import ast
output = pd.DataFrame(pd.read_csv('file.txt',sep='|',header=None).iloc[:,0]
.apply(ast.literal_eval).tolist())
Run Code Online (Sandbox Code Playgroud)
print(output)
cid text time author
0 ABCD alphabets 1 week xyz
1 EFGH verb 2 week aaa
2 IJKL noun 3 days nop
Run Code Online (Sandbox Code Playgroud)
工作示例:
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()))
Run Code Online (Sandbox Code Playgroud)
cid text time author
0 ABCD alphabets 1 week xyz
1 EFGH verb 2 week aaa
2 IJKL noun 3 days nop
?
Run Code Online (Sandbox Code Playgroud)