Here's some sample code that reads a text file containing a JSON string and converts it to a dictionary using the json module:

import json

# Open the file containing the JSON string
with open('file.txt', 'r') as file:
    # Read the contents of the file
    contents = file.read()

# Parse the JSON string into a dictionary
data = json.loads(contents)

# Now you can access the data as a dictionary
print(data['name'])
print(data['age'])

Assuming the file contains a JSON string like this:

{"name": "John", "age": 30}

The output of the above code would be:

John
30
use pythonread a txt file that saved json string save the content as a dict

原文地址: http://www.cveoy.top/t/topic/bVU7 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录