0

I have a mistake that I can't solve.. have you got an advice?

[root@leemi wwwroot]# python3 DateRead.py
Traceback (most recent call last):
  File "DateRead.py", line 33, in <module>
    data_ls = json.load(data_file)["Person"].values()
AttributeError: 'list' object has no attribute 'values'

Code created in VS.

import os
import datetime
import psycopg2
import inspect
import json
import io
from collections import OrderedDict

mtime = os.path.getmtime("data.json")
with io.open("saveDate.txt", "r", encoding="utf8") as date_read:
    ttime = date_read.read()

if str(mtime) == ttime:
    with io.open('data.json', "r", encoding="utf-8") as data_file:    
        data_ls = json.load(data_file)["Person"].values()
    # datas1 = OrderedDict(sorted(data_ls["Person"].itemsc(), key=lambda t: t[0]))3
    # print(datas1)
    datas = list(data_ls)
    # datas2 = [str(datas["PersonSex"]), str(datas["PersonAge"]), str(datas["PersonDate"])]
    print(datas[0],datas[1],datas[2])

    with io.open("saveDate.txt","w",encoding="utf-8") as date_save:
        date_save.write("{0}".format(mtime))
    print("INSERT INTO public.\"DbPerson\"(\"PersonSex\", \"PersonAge\", \"PersonDate\") VALUES ('{0}',{1},'{2}');".format(datas[0], datas[1], datas[2]))

VS has no errors. However, an error occurs at run time in Centos 7. As I'm a beginner of Python, I've searched for many things, but I can't solve them. Please help me.

Result

F 40 2020-05-01 8:10:00
INSERT INTO public."DbPerson"("PersonSex", "PersonAge", "PersonDate") VALUES ('F',40,'2020-05-01 8:10:00');

1 Answer 1

1

Apparently json.load(data_file)["Person"] is already a list. And in Python list types don't have any values() method. This is essentially what the error message is telling you. VS has no idea about the data structure so it cannot catch this error.

Try changing line 33 to:

data_ls = json.load(data_file)["Person"]
Sign up to request clarification or add additional context in comments.

3 Comments

print("INSERT INTO public.\"DbPerson\"(\"PersonSex\", \"PersonAge\", \"PersonDate\") VALUES ('{0}',{1},'{2}');".format(datas[0], datas[1], datas[2])) IndexError: list index out of range
Failed to get data from values() in Json file.
What does your JSON file look like?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.