0

I have a Python file that worked perfectly on my macbook. Once I moved it to window, for some reason it gave me new error.

this is the errors im getting:

Traceback (most recent call last):
  File "C:/Users/chadi/PycharmProjects/untitled4/main.py", line 3, in <module>
    from main_controller import Ui_MainController
  File "C:\Users\chadi\PycharmProjects\untitled4\main_controller.py", line 6, in <module>
    from get_companies import load_companies
  File "C:\Users\chadi\PycharmProjects\untitled4\get_companies.py", line 10, in <module>
    json_read('convertcsv.json')
  File "C:\Users\chadi\PycharmProjects\untitled4\get_companies.py", line 8, in json_read
    data = (json.load(f_in))
  File "C:\Users\chadi\anaconda3\envs\untitled4\lib\json\__init__.py", line 293, in load
    return loads(fp.read(),
  File "C:\Users\chadi\anaconda3\envs\untitled4\lib\json\__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "C:\Users\chadi\anaconda3\envs\untitled4\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\chadi\anaconda3\envs\untitled4\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Process finished with exit code 1

this is all the parts where i load a JSON file:

def json_read(filename):
    with open(filename) as f_in:
        global data
        data = (json.load(f_in))

json_read('convertcsv.json')

def mark_employee_unavailability(service,employee,reason,start_date,end_date):
    with open('info_inspecteurs.json') as json_file:
        data = json.load(json_file)
        for i in range(len(data)):
            if data[i]['n_inspecteur'] == employee:
                event_email = data[i]['email_inspecteur']
                break
def json_read(filename):
    with open(filename) as f_in:
        global data
        data = (json.load(f_in))

def get_employee_info(n_inspecteur):
    json_read('info_inspecteurs.json')
    value = list(filter(lambda x: x["n_inspecteur"] == n_inspecteur, data))[0]
    if len(value) > 0:
        print(value['ad_inspecteur'], "\n", value['email_inspecteur'])
        return (value['ad_inspecteur'],value['email_inspecteur'])
    print("no record found")
    return None

def load_employees_from_info_inspecteurs():
    json_read('info_inspecteurs.json')
    employees=[]
    for company in data:
        employees.append(company['n_inspecteur'])
    return employees

I don't know where it came from, or maybe is the new environement I used on Windows? I used anaconda. Does it change anything? I was on VENV on my macOS.

Thank you for your help

Chadi N
  • 439
  • 3
  • 13
  • What is the JSON you are trying to parse? – Scott Hunter Jun 17 '20 at 00:51
  • 1
    Please please please :) Make json_load _return_ the data instead of using a global variable. :-) – Marcello Romani Jun 17 '20 at 00:52
  • @MarcelloRomani Thank you for advice, but my project needs it to be global. – Chadi N Jun 17 '20 at 01:27
  • Load json should be a self contained function that takes a json file as input and returns its contents. Using a global variable like that is simply wrong. If you really need to, yoj can assign the global variable outside that function. – Marcello Romani Jun 17 '20 at 01:53
  • 1
    Ok I will look into it, but for now, the error im getting is : 'FileNotFoundError: [Errno 2] No such file or directory: 'convertcsv.json' ' even though i can physically see this file in the directory – Chadi N Jun 17 '20 at 02:04

1 Answers1

1

The error

json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

may just mean that there's no data being read from the target file at all, so json isn't finding any data to parse.

You might troubleshoot by trying to just read the file from that point in your script into a string - if it fails, the problem may be in python execution current directory / relative file path, rather than in the json parsing.

with open ("convertcsv.json", "r") as checkFile:
    checkData = checkFile.read()
    # print out length or contents of checkData or sim.
Corbell
  • 1,283
  • 8
  • 15
  • I did what you proposed, and I got 'FileNotFoundError: [Errno 2] No such file or directory: 'convertcsv.json' ' even though i can physically see this file in the directory? – Chadi N Jun 17 '20 at 01:30
  • 1
    I think that means the current working directory the python script is running under is not where you think it is. Use os.getcwd() to print where the script is actually executing from, and either use os.chdir() to change it or use some other method to get full paths to the input files. More here: https://stackoverflow.com/questions/8248397/how-to-know-change-current-directory-in-python-shell – Corbell Jun 17 '20 at 04:00