Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

427
Views
¿Leer el archivo json como marco de datos de pandas?

Estoy usando python 3.6 e intento descargar el archivo json (350 MB) como marco de datos de pandas usando el código a continuación. Sin embargo, me sale el siguiente error:

 data_json_str = "[" + ",".join(data) + "] "TypeError: sequence item 0: expected str instance, bytes found

¿Cómo puedo corregir el error?

 import pandas as pd # read the entire file into a python array with open('C:/Users/Alberto/nutrients.json', 'rb') as f: data = f.readlines() # remove the trailing "\n" from each line data = map(lambda x: x.rstrip(), data) # each element of 'data' is an individual JSON object. # i want to convert it into an *array* of JSON objects # which, in and of itself, is one large JSON object # basically... add square brackets to the beginning # and end, and have all the individual business JSON objects # separated by a comma data_json_str = "[" + ",".join(data) + "]" # now, load it into pandas data_df = pd.read_json(data_json_str)
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Según su código, parece que está cargando un archivo JSON que tiene datos JSON en cada línea separada. read_json admite un argumento de lines para datos como este:

 data_df = pd.read_json('C:/Users/Alberto/nutrients.json', lines=True)

Nota
Quite lines=True si tiene un solo objeto JSON en lugar de objetos JSON individuales en cada línea.

over 4 years ago · Santiago Trujillo Report

0

Usando el módulo json, puede analizar el json en un objeto python, luego crear un marco de datos a partir de eso:

 import json import pandas as pd with open('C:/Users/Alberto/nutrients.json', 'r') as f: data = json.load(f) df = pd.DataFrame(data)
over 4 years ago · Santiago Trujillo Report

0

Si abre el archivo como binario ( 'rb' ), obtendrá bytes. Qué tal si:

 with open('C:/Users/Alberto/nutrients.json', 'rU') as f:

Además, como se indica en esta respuesta, también puede usar pandas directamente como:

 df = pd.read_json('C:/Users/Alberto/nutrients.json', lines=True)
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!