英文:
How to convert text file containing python dictionary into an actual one
问题
I have a text file that looks like so:
```python
[{
"x": 0.37375009059906006,
"y": 0.858906626701355,
"y": 1.2558532880291295e-08,
},
{
"x": 0.4551462233066559,
"y": 0.8060519695281982,
"y": -0.023612480610609055,
},
{
"x": 0.5198760032653809,
"y": 0.7056148648262024,
"y": -0.0391654446721077,
},
etc, etc
And I want to know how to convert it into a proper list in python which stores the proper dictionaries.
My current code looks like this:
def create_array(type):
path = f'data/hands/{type}'
files = os.listdir(path)
data = []
for file in files:
with open(f'{path}/{file}', 'r') as f:
c_data = json.loads(f.read())
data.append(c_data)
data = [dict(x) for x in data]
print(data)
But I'm just getting this error:
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 5 column 1 (Char 82)
Anyone know how to get it working?
<details>
<summary>英文:</summary>
I have a text file that looks like so:
[{
"x": 0.37375009059906006,
"y": 0.858906626701355,
"y": 1.2558532880291295e-08,
},
{
"x": 0.4551462233066559,
"y": 0.8060519695281982,
"y": -0.023612480610609055,
},
{
"x": 0.5198760032653809,
"y": 0.7056148648262024,
"y": -0.0391654446721077,
},
etc, etc
And I want to know how to convert it into a proper list in python which stores the proper dictionaries.
My current code looks like this:
def create_array(type):
path = f'data/hands/{type}'
files = os.listdir(path)
data = []
for file in files:
with open(f'{path}/{file}', 'r') as f:
c_data = json.loads(f.read())
data.append(c_data)
data = [dict(x) for x in data]
print(data)
But I'm just getting this error:
`json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 5 column 1 (Char 82)`
Anyone know how to get it working?
</details>
# 答案1
**得分**: 0
由于你正在将文件解析为JSON,你遇到了一个问题,这是由于每个字典的最后一行(第二个`"y"`值)末尾有逗号引起的异常,所以你需要删除这些额外的逗号。像这样:
```json
[
{
"x": 0.37375009059906006,
"y": 0.858906626701355,
"y": 1.2558532880291295e-08
},
{
"x": 0.4551462233066559,
"y": 0.8060519695281982,
"y": -0.023612480610609055
},
{
"x": 0.5198760032653809,
"y": 0.7056148648262024,
"y": -0.0391654446721077
}
]
此外,请记住你正在将这些数据存储在字典中,所以你应该避免重复的键,否则重复的键将采用最后读取的值。因此,我强烈建议将每个字典中的最后一个"y"
更改为另一个键,例如:"z"
。
最后,你的列表data
是不必要的,因为json.loads()
将返回一个字典的列表。看看你的代码的简化版本:
def create_array(path):
with open(path, 'r') as f:
c_data = json.loads(f.read())
print(c_data)
这将输出:
[{'x': 0.37375009059906006, 'y': 1.2558532880291295e-08}, {'x': 0.4551462233066559, 'y': -0.023612480610609055}, {'x': 0.5198760032653809, 'y': -0.0391654446721077}]
请注意,由于重复键"y"
的存在,值0.858906626701355、0.8060519695281982和0.7056148648262024被分别替换为1.2558532880291295e-08、-0.023612480610609055和-0.0391654446721077。
英文:
As you are reading the file as JSON, you are getting an exception due to the ,
at the end of the last line on each dictionary (the second "y"
value), so you have to remove these last extra commas. Like this:
[
{
"x": 0.37375009059906006,
"y": 0.858906626701355,
"y": 1.2558532880291295e-08
},
{
"x": 0.4551462233066559,
"y": 0.8060519695281982,
"y": -0.023612480610609055
},
{
"x": 0.5198760032653809,
"y": 0.7056148648262024,
"y": -0.0391654446721077
}
]
Also, bear in mind that you are storing this data in dictionaries, so you should avoid repeating keys, otherwise, the duplicated ones will take the last value read. So I strongly recommend changing the last "y"
on each dictionary to another key, e.g.: "z"
.
Finally, your list data
is not necessary as the json.loads()
will return a list of dictionaries. Look at this simplified version of your code:
def create_array(path):
with open(path, 'r') as f:
c_data = json.loads(f.read())
print(c_data)
This outputs:
[{'x': 0.37375009059906006, 'y': 1.2558532880291295e-08}, {'x': 0.4551462233066559, 'y': -0.023612480610609055}, {'x': 0.5198760032653809, 'y': -0.0391654446721077}]
Note how the values 0.858906626701355, 0.8060519695281982, and 0.7056148648262024 were replaced by 1.2558532880291295e-08, -0.023612480610609055, and -0.0391654446721077 respectively because of the duplicated key "y"
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论