Ranish Byanjankar
Ranish Byanjankar

Reputation: 13

JSON to CSV for Python

I know there are many question to this but I am having trouble manipulating the data.

I have this list:

['2017-05-31,20:00:00,71.1,73,', '2017-05-31,20:05:00,71.1,72.7,', '2017-05-31,20:10:00,71.1,72.5,', '2017-05-31,20:15:00,71.1,72.4,']

I need to convert this JSON format to a CSV where the CSV looks like so.

enter image description here

Upvotes: 0

Views: 55

Answers (2)

Trolldejo
Trolldejo

Reputation: 466

I'd suggest:

my_list = ['2017-05-31,20:00:00,71.1,73,', '2017-05-31,20:05:00,71.1,72.7,', '2017-05-31,20:10:00,71.1,72.5,', '2017-05-31,20:15:00,71.1,72.4,']
# create and open a file for writing
with open("my_file.csv", "w") as fout:
    # iterate through your list
    for element in my_list:
        # write your element in your file plus a \n to trigger a new line
        fout.write(element+"\n")

et voilà!

Upvotes: 2

Tilak Putta
Tilak Putta

Reputation: 778

import pandas as pd
result = []
for item in myList:
    row = item.split(',')
    result.append(row)
df = pd.DataFrame(result)
df.to_csv("myFile.csv", index = False) 

Upvotes: 1

Related Questions