0

I am very new to python and I have csv problem. I have a code to extract data from Futbin website but I don't find how to do to export these datas to a csv file. The datas shown in Python are exactly what I need so the last step for me is to exctract them.

Here is the code :

import requests
import csv
from datetime import datetime

player_ids = {  
  'Maguire OTW': 50534911,  
  'Pulisic OTW' : 50559444,

}

for (name,id) in player_ids.items():
    r = requests.get('https://www.futbin.com/20/playerGraph?type=yesterday&year=20&player={0}'.format(id))
    data = r.json()

    print()
    print(name)   
    #Change ps to xbox or pc to get other prices
    for price in data['ps']:
        #There is extra zeroes in response.
        date = datetime.utcfromtimestamp(price[0] / 1000).strftime('%Y-%m-%d')
        price = price[1]
        print(date,price)

Thanks a lot for your help

Copy Comment: When I add this at the end of the code :

with open('yesterdayok.csv', 'w') as csv_file: 
    csv_writer = csv.writer(csv_file) 
    csv_writer.writerow(date) 

it creates the csv file but inside there's only 1 date, I need all the datas generated by the code.

stovfl
  • 14,998
  • 7
  • 24
  • 51
  • What data do you want to write to the csv file? In what format? – rassar Oct 30 '19 at 12:12
  • Consider using pandas to convert the JSON response to a csv. https://stackoverflow.com/questions/50558077/convert-json-to-csv-with-pandas – user9940344 Oct 30 '19 at 12:12
  • Does this answer your question? [Write CSV Python](https://stackoverflow.com/questions/33077478/write-csv-python) – Max Voitko Oct 30 '19 at 12:17
  • I'd like to write the datas I can see in python when I run this code to a csv file, nothing more. And I don't know how to code this. – David Marty Oct 30 '19 at 12:35
  • ***"inside there's only 1 date"***: [Edit] your question and show the output of `print(date)` before the line `.writerow(date)`. Read about [common-sequence-operations](https://docs.python.org/3/library/stdtypes.html#common-sequence-operations) – stovfl Oct 30 '19 at 13:24
  • In fact I want the date and the price to go to the csv file – David Marty Oct 30 '19 at 13:32
  • print(date,price) shows me exactly what I expect in python (several dates and prices) but I have no idea what to write in code to export these datas in a csv file – David Marty Oct 30 '19 at 13:35

0 Answers0