pandas python Load JSON from local file - python

I'm trying to set up a github so that all the code is self contained and the other authors don't need to post their entire path to certain files.
my code:
dataSet = pd.read_json("file://repository/Datasets/JSON/data.json", convert_dates=False)
This gives me this error:
URLError: <urlopen error [WinError 3] The system cannot find the path specified: '\\repository\\Datasets\\JSON\\data.json'>
As this is the way that the docs seem to describe how to do this, I'm stumped on how to do it

I'd say move the file into the same directory and simply use
dataSet = pd.read_json('data.json')
Once that works then you know for sure that it's not an issue with reading the file. The error suggests it's an issue with Windows reading the path to the file.
Not sure what editor you're using but in VS Code if you right click the file it allows you to copy the 'relative path' in relation to the file you're currently working on.
Sorry I can't be of more help.

Related

python how to import data in folder

hey guys i am new to python and have been trying to use google collaboratory notebook to learn pandas. i have been trying to import data but i was unable to do so, the error being :
`FileNotFoundError: [Errno 2] No such file or directory: './train.csv'`
but i had the csv file in my folder which my notebook is in.
This is the code i used to run. i had no idea why it doesnt work. Thanks for any suggestions.
train = pd.read_csv("./train.csv")
test = pd.read_csv("./test.csv")
Assuming you uploaded your files in Google colab correctly, I suspect that you're not using the exact location of the files (test.csv and
train.csv)
Once you navigate to the location of the files, find the location using
pwd
Once you find the location, you can read the files in pandas
train = pd.read_csv(Location_to_file)
test = pd.read_csv(location_to_file)

permission denied error while reading an excel file

i got a permission denied error while i tried to open an excel file.
I dont have the ms excel complete version. I mean, im just using the trial version.
Could it be because of that?
my code has just 4 lines
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
dataset = pd.read_excel("E:\\ML")
It's something about how file open function works. I successfully reproduced your problem and find the way.
It's believed that you have a directory named ML in E disk, and maybe there are some excels files (such as *.xls or *.xlsx) in ML(I bet you just started learning machine learning). Now you try to load the excel data into your program, but you give the path E:\\ML, which is a directory instead of a file, so operation is forbidden by system when pandas try to serialize the directory as a file, which is the cause of error "Permission denied".
The method is that you're supposed to use file path like E:\\ML\\your_database_file_name.xls.
I hope it will work for you.
For me, it turns out that it was because I had the same Excel file opened (I kept getting the error while trying to push my work to Github) which was resolved immediately after I closed the MS Excel (the program using the file I wanted to push..)
I hope you find this helpful!

Loading .npz file onto R

I am currently having a d.npz file that was born from Python that I would like to load it into R. I tried using the package RcppCNPy, but when I run load("d.npz"), it gave me this error message:
bad restore file magic number (file may be corrupted) -- no data loaded
I tried other tools like source("d.npz") or readRDS("d.npz"), but none worked out. Could anyone please help me solve this seemingly simple problem??

How to fix [Errno13] permission denied when trying to read excel file?

I tried the following code to be able to read an excel file from my personal computer.
import xlrd
book = xlrd.open_workbook('C:\\Users\eline\Documents\***\***\Python', 'Example 1.xlsx')
But I am getting the error 'Permission denied'. I am using windows and if I look at the properties of the directory and look at the 'Security' tab I have three groups/users and all three have permissions for all the authorities, except for the last option which is called 'special authorities' (as far as I know I do not need this authority to read the excel file in Python).
I have no idea how to fix this error. Furthermore, I do not have the Excel file open on my computer when running the simulation.
I really hope someone can help me to fix this error.
Sometimes, it is because you try to read the Excel file while it is opened. Close the file in Excel and you are good to go.
book = xlrd.open_workbook('C:\\Users\eline\Documents\***\***\Python', 'Example 1.xlsx')
You cannot give path like this to xlrd. path need to be single string.
If you insist you can use os module
import os
book = xlrd.open_workbook(os.path.join('C:\\Users\eline\Documents\***\***\Python', 'Example 1.xlsx'))
[Errno13] permission denied in your case is happening because you want to read folder like a file which is not allowed.
I ran into this situation also while reading an Excel file into a data frame. To me it appears that it is a Python and/or Excel bug which we should probably not hide by using os.path.join even if that solves the problem. My situation involved an excel spreadsheet that links cells to another CSV file. If this excel file is freshly opened and open when I try to read it in python, it fails.
Python reads it correctly if I do an unnecessary save of the open Excel file.

Python UDF - import/read external files

I would like to import other python/csv files into my python udf to perform some operations.
Like,
Comparing the table data(which flows in as a stream, row by row) to an external .csv row.
When I try to read data of .csv file, it gives me an error
IOError: File /home/abc/xyz/myfile.csv does not exist
While the code works perfectly well when it is written as a regular python script (not like udf)
If I understood it right . You can try
ADD FILE [Your complete file path]
or
Add FILES [Your directory path].
Because before referring anything on cluster you must add it to the distribution cache so that code there can access that portion.
you can have a look at it.
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+Cli
Be careful about the syntax! It may cause many problems and unfortunately, query language interpreter is not able to show where the problem is coming from and it just shows some generic error report.
Look at kind of the same problem here that was caused by a syntax issue in addressing the file!
Accessing external file in Python UDF

Categories

Resources