简体   繁体   中英

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

  • put the header "code","img","data"
  • separate each data in each column,
  • use a semi colon; as separator**
  • Have multiple URL's to call: urllib.request.urlopen("https://barcode.monster/api/3061990141101", https://barcode.monster/api/3061990141101","https://barcode.monster/api/3061990141101") as url's

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM