I have a .txt
file with a special format, namely, each line contains a header and content. Please see the following picture. I want to ask how I can import this file into Python via open or read_csv
functions. enter image description here
Looks like your file is formatted as a JSON file. You can use Python's json
module to handle JSON data as objects in Python using json.loads()
Here's a usage example, it may require some tweaking depending on how your JSON file is structured.
import json
with open("file.json", "r") as dataFile:
jsonData = json.loads(dataFile.read())
for item in jsonData:
print(item["id"])
This code was tested with the following JSON data:
[
{
"id": 1,
"name": "Example 1"
},
{
"id": 1,
"name": "Example 2"
}
]
You can read more about the json
module here: https://docs.python.org/3/library/json.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.