mlclm
mlclm

Reputation: 725

Python + Pandas get JSON data from multiple URLs to write CSV in separate columns with semi colon as separator

I could get some data and write to CSV , but the data is put in one single column. I have not been able to use columns = ["code", "img", "data"]) as a header. Not sure if that is related to the index problem I had earlier with it which was fixed (Value error if using all scalar values, you must pass an index.)

So I don't know if the index thing is now causing a problem to write a proper CSV but I couldn't find any info.

Basically only wanted to

My script :

import urllib.request, json, csv
import pandas as pd

with urllib.request.urlopen("https://barcode.monster/api/3061990141101") as url:
    data = json.loads(url.read().decode())

    # print(data)
with open('data.json', 'r') as f:

    data = json.load(f)

    df = pd.DataFrame(
        {'test': data})

    df.to_csv('test.csv', encoding='utf-8', index=False)

When I open the csv in notepad, there's 9 lines in total.:

test 
EAN13 
3061990141101 
mini BN Chocolate flavour - 25 biscuits (5 paquets)
https://courses-en-ligne-now.com/.jpg

So meaning this array has only one column and it's all in one column. All I wanted was something like 3 (or more) rows:

code | img | data | (more later)
----------------------------------------------------------------------
EAN13 | 3061990141101 | mini BN Chocolate flavour - 25 biscuits (5 paquets) | https://courses-en-ligne-now.com/media/Photosite/3061990141101_PHOTOSITE_20180726_045123_0.jpg

...and if I open that in notepad I would see just 2 rows , and each data would be separated with a semi colon.

Any help would be much appreciated.

Upvotes: 1

Views: 577

Answers (1)

sushanth
sushanth

Reputation: 8302

try this,

import requests

urls = ["https://barcode.monster/api/3061990141101",]

result = []
for url in urls:
    resp = requests.get(url)
    if resp.status_code != 200:
        print(f"Error {url}")
        continue

    result.append(resp.json())

pd.DataFrame(result).to_csv('test.csv', encoding='utf-8', index=False, sep=";")

   class           code  ... size  status
0  EAN13  3061990141101  ...       active

Upvotes: 1

Related Questions