Problems Importing a .json file in PyCharm [duplicate] - python

This question already has answers here:
How do I read a json file into python?
(2 answers)
Closed 1 year ago.
I have a main.py file and an OrientationData.json file in a PyCharm project. I would like to import my data into main.py, but for some reason it will not allow me to. I have json imported in main.py, and both files are in the same venv folder of the project. If I try to import a .py file into main.py, I have no issues, but it simply does not recognize the json file when I try to import it.
(.py imports working just fine)
(.json import not working)
I have tried writing it as import OrientationData.json, but that also does not work. The specific error it's giving me is "no module named OrientationData" when I hover over the import statement. Why is this happening, and what can I do so that I can import and use my json data?

I don't think that is the proper way to read a .json file in python. If you are trying to read the .json file for your code, try this:
f = open('OrientationData.json', 'r')
data = json.load(f)
God bless

Try this:
Import json
file=open('data.json',r)
data=json.load(file.read)
#remember to close your file at the end
#or use a with block

Related

No such file or directory found, Python [duplicate]

This question already has answers here:
Open file in a relative location in Python
(14 answers)
Closed 7 months ago.
I'm new to coding so I followed a tutorial for a chatbot, I need to import my intentions (which I named intentii, in my language) intentii.json into the chatbot, so I'm using:
intentii = json.loads(open('intentii.json').read())
I have seen other questions about the error I said in the title, and yes, I made sure the name is typed right, it's in the same exact directory as all my files including this chatbot one are, and still, it says it can't find the file, I tried placing the whole directory path and it seemed to work ( I didn't get the same error again, but an unicode error which I believe is another problem related to my .json file ), but I cannot use the whole path because I have to send this to my teacher and, of course, he won't have the same exact path as mine.
What is the solution?
Edit: Also, I've noticed that it says that for every file I need to access from the folder
Try this function to load a json file:
import json
def load_json(filepath):
with open(filepath) as f:
data = json.load(f)
return data
Then
intentii = load_json("intentii.json")
Try using this python os module to find dir_root where current file is located and then combine it json file name if it is stored in same directory.
import json
import os
dir_root = os.path.dirname(os.path.abspath(__file__))
intentii1 = json.loads(open(dir_root+'\js.json').read())

Importing file (without extension) contents into python script as object

I have a file titled "passc" (no .txt, no .py, no extension at all) that contains a single line of code (its a token) similar to:
aABBccDd01234
Yes, there are no quotes or variable assignment in this file
I am trying to import this value into a python script and assign it to a variable (I think it should look something like this):
import passc
code = passc
I have tried variations of the following:
from passc import *
code = passc[0]
And I understand I'm importing a module not a direct object so it shouldn't have a subscriptable element, but I thought I might as well try it.
Without defining the value in "passc" file I can't figure out how to access the value.
Looking for suggestions on how to access this value without altering the "passc" file!
Do not complicate yourself using import passc or from passc import * of this way only we can import files with .py extension.
The fast solution to fix this problem is doing something like this:
# read file
my_file=open("passc","r")
# save code in a var
code = my_file.read()
print(code)
# aABBccDd01234
Cheers!

I am trying to read a csv file using pandas but it fails to recognize it - what could be the issue? (Tkinter)

I have been trying to read this csv file using pandas and then transfer it to a dictionary using:
pandas.read_csv("/Users/vijayaswani/Downloads/England1\ postcodes.csv ", index_col=1).T.to_dict()
but each time I get the error No such file or directory
neither does using the name of the file work and nor does using its path even though the file is not deleted or anything.
What could be the issue?
Looks like you have an extra space in the file path.
Have you tried:
pandas.read_csv("/Users/vijayaswani/Downloads/England1\ postcodes.csv", index_col=1).T.to_dict()
Or
pandas.read_csv("/Users/vijayaswani/Downloads/England1 postcodes.csv", index_col=1).T.to_dict()

How to use json file that placed in the python project?

I have project in Pycharm, and I want to use json file that placed in this project.
How can I call it?
I use this one:
import json
file = open('~/PycharmProjects/Test/JSON_new.json')
x = json.load(file)
And received the following error:
FileNotFoundError: [Errno 2] No such file or directory: '~/PycharmProjects/Test/JSON_new.json'
But path is correct
EDIT: I understood what is the problem. Instead of json file txt was created (but I selected json). It creates txt files, maybe, someone knows hot to solve it? I can create only .py files directly. Other files no.
Is it correct if I create scratch json file and placed it in Scratches?
You may like to use following path(In linux):
file = open('/home/<user>/PycharmProjects/Test/JSON_new.json')
Replace user with your username. You need to know the correct path to the file, for which you can user PWD command in terminal.
You can use json module for this. You can open the file in a seperate object and pass it to json.load if you have a JSON string use json.loads.
import json
file = open('/path/to/json/file.json')
file_opened = json.load(file)

python: import module from different file directory, with folder names having spaces

I already know this will be (wrongly, in my opinion) labelled as duplicate because of this or this, which I've already looked at.
However my problem is different, and cannot be solved with the answers found there.
So I have a python script in C:\Users\Me\Desktop\My folders\Projects\My stuff\Last Folder\some_file.py. From this I want to import a file in the same directory, i.e. inside Last Folder, for this I just use import file.
However, then I have a file in C:\Users\Me\Desktop\My Other Folder\My Other Projects\ My Other Stuff\other_file.py. As you can see the folders have spaces.
Now in the second of the two links above we are told how to import a python file with spaces in the name, a python file, not a folder!
In the second link, the user wants to just open a file using os.
I instead want to import it. How do I do it? When I try
import Users.Me.Desktop.My Other Folder.My Other Projects.My Other Stuff.other_file.py
it says "invalid syntax" because of the spaces. How can I solve this?
import sys
sys.path.append('\Users\Me\Desktop\My folders\Projects\My stuff\Last Folder\some_file.py.')
# From this I want to import a file in the same directory, i.e. inside Last Folder, for this I just use import file. However, then I have a file in C:\Users\Me\Desktop\My Other Folder\My Other Projects\ My Other Stuff
from other_file import *
I needed to import a utils.py file into a Google Colab Notebook and this worked for me, combining the solution from Surya and here.
from google.colab import drive
drive.mount('/content/drive')
import sys
sys.path.append("/content/drive/My Drive/Colab Notebooks") # note the absolute path
print(sys.path)
from utils import a, b, c

Categories