Content of a Sample Input Text
{'key1':'value1','msg1':"content1"} //line 1
{'key2':'value2','msg2':"content2"} //line 2
{'key3':'value3','msg3':"content3"} //line 3
Also, pointing out some notable characteristics of the input text
- Lacks a proper delimiter, currently each object {...} takes a new line "\n"
- Contains single quotes, which can be an issue since JSON (the expected output) accepts only double quotes
- Does not have the opening and closing curly brackets required by JSON
Expected Output JSON
{
{
"key1":"value1",
"msg1":"content1"
},
{
"key2":"value2",
"msg2":"content2"
},
{
"key3":"value3",
"msg3":"content3"
}
}
What I have tried, but failed
- json.dumps(input_text), but it cannot identify "\n" as the "delimiter"
- Appending a comma at the end of each object {...}, but encountered the issue of extra comma when it comes to the last object
CodePudding user response:
If you have one dictionary per line, you can replace newlines with ,
and enclose the whole in brackets [
,]
(you get a list of dictionaries).
You can use ast.literal_eval
to import your file as list of dictionaries.
Finally export it to json:
import json
import ast
with open("file.txt", "r") as f:
dic_list = ast.literal_eval("[" f.read().replace('\n',',') "]")
print(json.dumps(dic_list, indent=4))
Output:
[
{
"key1": "value1",
"msg1": "content1"
},
{
"key2": "value2",
"msg2": "content2"
},
{
"key3": "value3",
"msg3": "content3"
}
]
CodePudding user response:
Just use ast
import ast
with open('test.txt') as f:
data = [ast.literal_eval(l.strip()) for l in f.readlines()]
print(data)
output
[{'key1': 'value1', 'msg1': 'content1'}, {'key2': 'value2', 'msg2': 'content2'}, {'key3': 'value3', 'msg3': 'content3'}]