0

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

3
  • 1
    If you have trouble with your code, why didn't you write a question that includes that code, and asks for help fixing it? Instead of writing a question that reads like: "please do my work for me"? Hint: read minimal reproducible example and meta.stackoverflow.com/questions/284236/…
    – GhostCat
    Commented Jul 3, 2017 at 13:58
  • 1
    So, what have you tried? Exactly what "trouble" did you have? Commented Jul 3, 2017 at 13:59
  • It looks like a list rather than JSON. Also what is the trouble? did you use the csv module? Commented Jul 3, 2017 at 14:01

2 Answers 2

2

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à!

1
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) 
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.