1

При компиляции данного кода:

import json

user1 = {
'user_Name1':"Maxim",
'Money1':100
}

user2 = {
'user_Name2':"Sasha",
'Money':101
}

users = []
users.append(user1)
users.append(user2)

f = open('BD.txt','a')
# f.write(str(users))

json.dump(users, f)

f2 = open('BD.txt','r')
json_storage = json.load(f2)

for i in json_storage:
    print("Name is:" + str(user1['user_Name1']))
    print("Money:"+ str(user1['Money1']))

Выдает ошибку:

Traceback (most recent call last):
  File "source_file.py", line 23, in <module>
    json_storage = json.load(f2)
  File "/usr/lib/python3.5/json/__init__.py", line 268, in load
    parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, 
**kw)
  File "/usr/lib/python3.5/json/__init__.py", line 319, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.5/json/decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.5/json/decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Пытался скомпилировать заменив некоторые строки на это:

f = open('BD.txt', 'a', encoding='utf-8')
f2 = open('BD.txt', encoding='utf-8')

Но опять же выдает ошибку, но другую:

OSError: [Errno 27] File too large

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "source_file.py", line 18, in <module>
    json.dump(users, f)
OSError: [Errno 27] File too large

2 ответа 2

0

Ошибка OSError выдаётся когда у вас запущен тот же сервер в двух местах. У меня возникала ошибка когда я запускал сервер на vscode потом на Pycharm не остонавливая процесс на vscode.

0

Укажите кодировку, заменив на это:

f = open('BD.txt', 'a', encoding='utf-8')
f2 = open('BD.txt', encoding='utf-8')

И я бы рекомендовал так писать:

import json

user1 = {
    'user_Name1': "Maxim",
    'Money1': 100
}

user2 = {
    'user_Name2': "Sasha",
    'Money': 101
}

users = []
users.append(user1)
users.append(user2)

with open('BD.txt', 'a', encoding='utf-8') as f:
    json.dump(users, f)

with open('BD.txt', encoding='utf-8') as f:
    json_storage = json.load(f)

for i in json_storage:
    print("Name is:", user1['user_Name1'])
    print("Money:", user1['Money1'])
6
  • Выдает ошибку: OSError: [Errno 27] File too large During handling of the above exception, another exception occurred: Traceback (most recent call last): File "source_file.py", line 18, in <module> json.dump(users, f) OSError: [Errno 27] File too large
    – SuNøt
    16 окт 2018 в 13:39
  • @SuNøt, какая-то ошибка у вас?
    – gil9red
    16 окт 2018 в 13:40
  • выдает другую ошибку.
    – SuNøt
    16 окт 2018 в 14:03
  • @SuNøt, ну бывает такое с кодом... а ошибка то какая? :)
    – gil9red
    16 окт 2018 в 14:20
  • я же написал ошибку... :(
    – SuNøt
    16 окт 2018 в 15:42

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.