如何将包含Python字典的文本文件转换为实际字典

huangapple go评论64阅读模式
英文:

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&#39;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,你遇到了一个问题,这是由于每个字典的最后一行(第二个`&quot;y&quot;`值)末尾有逗号引起的异常,所以你需要删除这些额外的逗号。像这样:

```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
    }
]

此外,请记住你正在将这些数据存储在字典中,所以你应该避免重复的键,否则重复的键将采用最后读取的值。因此,我强烈建议将每个字典中的最后一个&quot;y&quot;更改为另一个键,例如:&quot;z&quot;

最后,你的列表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}]

请注意,由于重复键&quot;y&quot;的存在,值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 &quot;y&quot; value), so you have to remove these last extra commas. Like this:

[
    {
    &quot;x&quot;: 0.37375009059906006,
    &quot;y&quot;: 0.858906626701355,
    &quot;y&quot;: 1.2558532880291295e-08
    },
    {
    &quot;x&quot;: 0.4551462233066559,
    &quot;y&quot;: 0.8060519695281982,
    &quot;y&quot;: -0.023612480610609055
    },
    {
    &quot;x&quot;: 0.5198760032653809,
    &quot;y&quot;: 0.7056148648262024,
    &quot;y&quot;: -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 &quot;y&quot; on each dictionary to another key, e.g.: &quot;z&quot;.

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, &#39;r&#39;) as f:
        c_data = json.loads(f.read())
    print(c_data)

This outputs:

[{&#39;x&#39;: 0.37375009059906006, &#39;y&#39;: 1.2558532880291295e-08}, {&#39;x&#39;: 0.4551462233066559, &#39;y&#39;: -0.023612480610609055}, {&#39;x&#39;: 0.5198760032653809, &#39;y&#39;: -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 &quot;y&quot;.

huangapple
  • 本文由 发表于 2023年2月8日 18:21:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75384345.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定