0

I have below code

import requests, json

r = requests.get('http://someurl.com/api')
data = r.json()
flights = []
for flight in data:
    # print(flight['identifier'])
    if flight['STDudt'].strip() < flight['ETDudt'].strip() and flight['OUTudt'].strip() == '':
        flights.append(flight)

print(flights)

How can I convert it to csv file with keys of object as column names?

output i get from code looks like this:

[{'numGMTDate': '20180515', 'STDudt': '1405', 'STAudt': '1545', 'ETDudt': '1415', 'OUTudt': '', 'OFFudt': ''},{'numGMTDate': '20180515', 'STDudt': '1530', 'STAudt': '1831', 'ETDudt': '1540', 'OUTudt': '', 'OFFudt': ''}]
jpp
  • 159,742
  • 34
  • 281
  • 339
jeesan485
  • 147
  • 5
  • 14
  • 3
    Possible duplicate of [Python convert JSON to CSV](https://stackoverflow.com/questions/30454216/python-convert-json-to-csv) – zwer May 15 '18 at 21:08
  • 2
    Also possible duplicate of [How do I convert this list of dictionaries to a csv file?](https://stackoverflow.com/questions/3086973/how-do-i-convert-this-list-of-dictionaries-to-a-csv-file) – zwer May 15 '18 at 21:09

2 Answers2

0

One way that I know to go about this problem requires a bit of work, first save the python code to a place where you can easily set up a file. Now I am pretty lengthy with this and there are probably other ways but.

outfile = open ("STRING NAME.csv","w")

Will create the CSV file by "String Name" the .csv will make the file easier to open

outfile.write(value)
outfile.write(",")

will separate everything by commas and at the end of each line that you want to write

outfile.write("\n")

will make a new line for the csv

outfile.close()

to finish it off

hope this helps

example of where I have used before

        while h<=math.sqrt(float(d)):
        if float(d/(h*h))!=float(int(d/(h*h))):
            h=h+1
        else:
            j=h
            h=h+1
    if j==int(1) or int(d/(j*j))==1:
        a=a
    else:
        if j/2==int(j/2):
            a=a
        else:     
            outfile.write(str(int(b)))
            outfile.write(",")
            outfile.write(str(int(c)))
            outfile.write(",")
            outfile.write(str(int(d)))
            outfile.write(",")
            outfile.write(str(int(g)))
            outfile.write(",")
            outfile.write(str(int(j)))
            outfile.write(",")
            outfile.write(str(int(d/(j*j))))
            outfile.write(",")
            print("b=",int(b)," and c=",c)
            print(float(d)," has a square root of ",float(j)," rad ",float(d/(j*j)))
        a=a+1
    if a==16:
        z=input("SO....")
        if z==str("good"):
            a=16
        else:
            a=1
            print("Fui")
            outfile.write("Bad")
            outfile.write("\n")
    while a==16 and p<10:
        outfile.write("\n")
        p=p+1
        print("new line")
        a=1
outfile.close()
0

I'm almost there based on code found on suggested duplicate. Only problem is it leaves a blank row between each row.

import requests, json

    r = requests.get('http://someurl.com/api')
    data = r.json()
    flights = []
    for flight in data:
        # print(flight['identifier'])
        if flight['STDudt'].strip() < flight['ETDudt'].strip() and flight['OUTudt'].strip() == '':
            flights.append(flight)

    keys = flights[0].keys()
    with open('filteredData.csv', 'w') as output_file:
        dict_writer = csv.DictWriter(output_file, keys)
        dict_writer.writeheader()
        dict_writer.writerows(flights)

enter image description here

jeesan485
  • 147
  • 5
  • 14