How to import the json file in python - python

I am trying to import a json file to python and then export is to an excel file using the following code:
import pandas as pd
df = pd.read_json('pub_settings.json')
df.to_excel('pub_settings.xlsx')
but i am getting the following error:
can anyone please tell me what i am doing wrong?

First import json file as a dictionary using following code:-
import json
with open("") as f:
data = json.load(f)
Then you can use following link to convert it to xlsx:-
https://pypi.org/project/tablib/0.9.3/

Related

How do you import a ndjson file in jupyter notebook

I have tried the code below but it's not working
import json
with open("/Users/elton/20210228test2.ndjson") as f:
test2data = ndjson.load(f)
This works for me. import ndjson instead of import json. See more here: https://pypi.org/project/ndjson/
import ndjson
# load from file-like objects
with open('data.ndjson') as f:
data = ndjson.load(f)

Read from website csv file with variable name

I am using Jupyter and I would like to read csv file from a web site.
The problem I'm facing is that this file changes the name according the time of . For example, if now is 11/21/2019, 02:45:33, than the name will be "Visao_329465_11212019_024533.csv".
So I can't use just this
import pandas as pd
url="https://anythint.csv"
c=pd.read_csv(url)
Returns the error: ParserError: Error tokenizing data. C error: Expected 1 fields in line 31, saw 2
Any ideia?
Try:
import pandas as pd
url="https://anythint.csv"
c=pd.read_csv(url, error_bad_lines=False)

Can't save data from yfinance into a CSV file

I found library that allows me to get data from yahoo finance very efficiently. It's a wonderful library.
The problem is, I can't save the data into a csv file.
I've tried converting the data to a Panda Dataframe but I think I'm doing it incorrectly and I'm getting a bunch of 'NaN's.
I tried using Numpy to save directly into a csv file and that's not working either.
import yfinance as yf
import csv
import numpy as np
urls=[
'voo',
'msft'
]
for url in urls:
tickerTag = yf.Ticker(url)
print(tickerTag.actions)
np.savetxt('DivGrabberTest.csv', tickerTag.actions, delimiter = '|')
I can print the data on console and it's fine. Please help me save it into a csv. Thank you!
If you want to store the ticker results for each url in different csv files you can do:
for url in urls:
tickerTag = yf.Ticker(url)
tickerTag.actions.to_csv("tickertag{}.csv".format(url))
if you want them all to be in the same csv file you can do
import pandas as pd
tickerlist = [yf.Ticker.url for url in urls]
pd.concat(tickerlist).to_csv("tickersconcat.csv")

How to load a json file in jupyter notebook using pandas?

I am trying to load a json file in my jupyter notebook
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib as plt
import json
%matplotlib inline
with open("pud.json") as datafile:
data = json.load(datafile)
dataframe = pd.DataFrame(data)
I am getting the following error
JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Please help
If you want to load a json file use pandas.read_json.
pandas.read_json("pud.json")
This will load the json as a dataframe.
The function usage is as shown below
pandas.read_json(path_or_buf=None, orient=None, typ='frame', dtype=True, convert_axes=True, convert_dates=True, keep_default_dates=True, numpy=False, precise_float=False, date_unit=None, encoding=None, lines=False, chunksize=None, compression='infer')
You can get more information about the parameters here
http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_json.html
Another way using json!
import pandas as pd
import json
with open('File_location.json') as f:
data = json.load(f)
df=pd.DataFrame(data)
with open('pud.json', 'r') as file:
variable_name = json.load(file)
The json file will be loaded as python dictionary.
This code you are writing here is completely okay . The problem is the .json file that you are loading is not a JSON file. Kindly check that file.

Python Pandas Not Recognizing JSON File

I'm trying to load JSON data in Pandas in order to do some analysis.
Here is an example of the data I'm analyzing.
http://data.ncaa.com/jsonp/game/football/fbs/2013/08/31/wyoming-nebraska/field.json
I have tried the following:
import json
import pandas as pd
from pandas import DataFrame
json_data = pd.read_json('jsonv3.json')
and also
import json
import pandas
from pprint import pprint
json_data=open('jsonv3.json')
data = json.load(json_data)
pprint(data)
json_data.close()
The resulting errors are as follows:
1) ValueError: Expected object or value
2) ValueError: No JSON object could be decoded
I don't really know why the JSON file is not being recognized.
I've confirmed on http://jsonformatter.curiousconcept.com/ That it is valid JSON. I don't really know how to debug the issue. I haven't been able to find anything. Is the error potentially because of the JSON spacing format?
That's not JSON, it is JSONP. Note that the JSON "content" is wrapped in a "function call" callbackWrapper(...). From the wikipedia article: "The response to a JSONP request is not JSON and is not parsed as JSON".
If you've saved the JSONP response in the file jsonv3.json, you could strip off the function call wrapper and process the content with something like this:
import json
with open('jsonv3.json', 'r') as f:
response = f.read()
start = response.find('(')
end = response.rfind(')')
json_content = response[start+1:end]
data = json.loads(json_content)

Categories

Resources