简体   繁体   中英

How to convert a txt file with dictionary format to dataframe in python?

I have a file comprising data like,

{"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"}

I wish to read this file and create a dataframe like,

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

You can try reading the file as csv with a different seperator and grabbing the first column , then apply ast.literal_eval to convert to actual dictionary and convert back to dataframe:

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

Working example:

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
​

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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