简体   繁体   中英

import text from txt separated by brackets into pandas dataframe

I have a txt file with data written from an api request where each entry is bracketed, like this:

[1514780160000, '13329.00000000', '13330.00000000', '13323.55000000', '13330.00000000', '11.49322200', 1514780219999, '153192.57770167', 68, '9.55175200', '127322.45754008', '0'], [1514780220000, '13330.00000000', '13389.98000000', '13328.90000000', '13335.05000000', '9.00107100', 1514780279999, '120042.83181642', 84, '5.78463600', '77160.15209041', '0']

how do I read this into pandas dataframe with read csv as when I try splitting by brackets it gives me an error? Thank you.

Cheers,

import pandas as pd

data = pd.read_csv('ss.txt', header=None)

print(data)

Or:

import pandas as pd
import ast

with open('ss.txt') as f:
    res = ast.literal_eval(f.read())
    print(pd.DataFrame(res))

try this,

import pandas as pd
import ast

f=open('s1.txt')
res = list(ast.literal_eval(f.read()))
print(pd.DataFrame(res))
f.close()

O/P:

              0               1  ...               10 11
0  1514780160000  13329.00000000 ...  127322.45754008  0
1  1514780220000  13330.00000000 ...   77160.15209041  0

[2 rows x 12 columns]

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