xopherwwl
xopherwwl

Reputation: 39

convert text to dataframe in python

I got my API response and made it into text. Below is the result:

'timestamp,open,high,low,close,volume\r\n2020-07-10 19:50:00,0.6020,0.6020,0.6020,0.6020,2436\r\n2020-07-10 19:40:00,0.6005,0.6005,0.6005,0.6005,1000\r\n2020-07-10 19:15:00,0.6200,0.6220,0.6200,0.6220,2200\r\n2020-07-10 19:05:00,0.6100,0.6100,0.6100,0.6100,1000\r\n2020-07-10
...' 

how do i convert this into csv? Also would it be possible to do it without having to save the csv file on my device?

Upvotes: 0

Views: 2200

Answers (2)

David Erickson
David Erickson

Reputation: 16673

If this answer is helpful, then go upvote the accepted answer here where I grabbed this from and slightly modified: How to convert string that uses "\r\n" as line breaks to pandas dataframe

import pandas as pd
s = b'timestamp,open,high,low,close,volume\r\n2020-07-10 19:50:00,0.6020,0.6020,0.6020,0.6020,2436\r\n2020-07-10 19:40:00,0.6005,0.6005,0.6005,0.6005,1000\r\n2020-07-10 19:15:00,0.6200,0.6220,0.6200,0.6220,2200\r\n2020-07-10 19:05:00,0.6100,0.6100,0.6100,0.6100,1000\r\n2020-07-10' 
data = list(map(lambda x: x.split(','),s.decode('utf-8').split("\r\n")))
df = pd.DataFrame(data[1:], columns=data[0])
df

output:

    timestamp           open    high    low    close    volume
0   2020-07-10 19:50:00 0.6020  0.6020  0.6020  0.6020  2436
1   2020-07-10 19:40:00 0.6005  0.6005  0.6005  0.6005  1000
2   2020-07-10 19:15:00 0.6200  0.6220  0.6200  0.6220  2200
3   2020-07-10 19:05:00 0.6100  0.6100  0.6100  0.6100  1000
4   2020-07-10          None    None    None    None    None

Upvotes: 0

Roy2012
Roy2012

Reputation: 12493

Your data is already a CSV. Just do the following, using StringIO:

import pandas as pd
from io import StringIO

data = 'timestamp,open,high,low,close,volume\r\n2020-07-10 19:50:00,0.6020,0.6020,0.6020,0.6020,2436\r\n2020-07-10 19:40:00,0.6005,0.6005,0.6005,0.6005,1000\r\n2020-07-10 19:15:00,0.6200,0.6220,0.6200,0.6220,2200\r\n2020-07-10 19:05:00,0.6100,0.6100,0.6100,0.6100,1000\r\n2020-07-10'
pd.read_csv(StringIO(data))

The output is:

             timestamp    open    high     low   close  volume
0  2020-07-10 19:50:00  0.6020  0.6020  0.6020  0.6020  2436.0
1  2020-07-10 19:40:00  0.6005  0.6005  0.6005  0.6005  1000.0
2  2020-07-10 19:15:00  0.6200  0.6220  0.6200  0.6220  2200.0
3  2020-07-10 19:05:00  0.6100  0.6100  0.6100  0.6100  1000.0
4           2020-07-10     NaN     NaN     NaN     NaN     NaN

Upvotes: 3

Related Questions