简体   繁体   English

尝试将 txt 文件中的数据加载到请求的变量中

[英]Trying to load data from a txt file into a variable for requests

with open('data.txt', 'r') as file:
     dat2 = file.read()

post2 = {
        "id": 5,
        "method": "set",
        "params": [
            {
                "data": [
                     dat2
                    ],
                "url": "/config/url"
            },
            ]
        "session": sessionkey,
        "verbose": 1
        }

Data from the file I am trying to read looks as so...我试图读取的文件中的数据看起来如此......

{"name": "Host1","type": "ipmask","subnet": ["0.0.0.0","255.255.255.255"],"dynamic_mapping": null}, {"name": "Host2","type": "ipmask","subnet": ["0.0.0.0","255.255.255.255"],"dynamic_mapping": null}, I am trying to read this data and insert into a variable to put it into post2 for a request. {"name": "Host1","type": "ipmask","subnet": ["0.0.0.0","255.255.255.255"],"dynamic_mapping": null}, {"name": "Host2","type": "ipmask","subnet": ["0.0.0.0","255.255.255.255"],"dynamic_mapping": null},我正在尝试读取这些数据并将其插入到变量中post2请求。 What I have tried so far includes: reading the file and replacing null with None so python can read it as well as stripping all of the whitespace.到目前为止我尝试过的内容包括:读取文件并将null替换为None以便 python 可以读取它以及剥离所有空白。 I have tried using json.loads() , json.load() and json.dumps() , but nothing seems to work.我曾尝试使用json.loads()json.load()json.dumps() ,但似乎没有任何效果。 When I try to use json.load() I get the following error.当我尝试使用json.load()时,出现以下错误。

File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\json\__init__.py", line 296, in load
    parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
  File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\json\__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\json\decoder.py", line 340, in decode
    raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 2 column 18 (char 145)

After the data is placed into dat2, it gets inserted into post2 as '{data}' instead of {data}.将数据放入 dat2 后,它会以 '{data}' 而不是 {data} 的形式插入 post2。 Also yes, I know that file.read() will read the contents of the file into a string, but I have been trying everything since I am struggling to have success using json.也是的,我知道file.read()会将文件的内容读入字符串,但是我一直在尝试一切,因为我正在努力使用 json 取得成功。 I have been stuck on this part of my code for the longest time now and would appreciate and ideas.我已经被困在我的代码的这一部分最长时间了,我会很感激和想法。 NOTE: I HAVE LOOKED AT MULTIPLE PYTHON/JSON POSTS FOR READING PYTHON AND NOTHING WORKS SO PLEASE DON'T MARK AS DUPLICATE.注意:我查看了多个 Python/JSON 帖子以阅读 PYTHON,但没有任何效果,因此请不要标记为重复。

Remove the trailing apostrophe and put the dictionaries in a list in your file, it should look like this:删除结尾的撇号并将字典放在文件的列表中,它应该如下所示:

[{"name": "Host1","type": "ipmask","subnet": ["0.0.0.0","255.255.255.255"],"dynamic_mapping": null}, {"name": "Host2","type": "ipmask","subnet": ["0.0.0.0","255.255.255.255"],"dynamic_mapping": null}]

Then use json.loads to turn it into a list:然后使用json.loads把它变成一个列表:

with open('data.txt', 'r') as file:
     dat2 = file.read()

import json
post2 = {"data": json.loads(dat2)}

And this will make post2 be这将使post2成为

{'data': [{'name': 'Host1', 'type': 'ipmask', 'subnet': ['0.0.0.0', '255.255.255.255'], 'dynamic_mapping': None}, {'name': 'Host2', 'type': 'ipmask', 'subnet': ['0.0.0.0', '255.255.255.255'], 'dynamic_mapping': None}]}

Hope this helps!希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM