def save(): // func to save data
with open("vrhsp.json", "w") as outfile:
j=0
for row_id in Trw.get_children():
rowki = Trw.item(row_id)['values']
#print(rowki[1])
#print(type(rowki)) //control data taking
dict0={ j:
{
"text": "j+1",
"image": "",
"values": rowki,
"open": 0,
"tags": ""},
}
j=+1
json.dump(dict0, outfile,indent=4, ensure_ascii=False)`
It is needed to treeview all data transport to a JSON file. It is important to get items from the treeview. Not one by one. All data gets collected and sent to .json in one shot. It must be .json file; it is not permitted to .csv. or other
The .json file is below:
{
{ "0":
{
"text": "1",
"image": "",
"values": [
2,
45,
677,
899,
"-241.0",
"10.05.2025",
"22.59"
],
"open": 0,
"tags": ""
}
"1": {
"text": "2",
"image": "",
"values": [
3,
33,
23,
22,
"-242.0",
"10.05.2025",
"22.59"
],
"open": 0,
"tags": ""
}
But I want a comma (,) in datas.
What I want is given below:
{{ "0":
{
"text": "1",
"image": "",
"values": [
2,
45,
677,
899,
"-241.0",
"10.05.2025",
"22.59"
],
"open": 0,
"tags": ""
} , // It should be a comma here !!
"1": {
"text": "2",
"image": "",
"values": [
3,
33,
23,
22,
"-242.0",
"10.05.2025",
"22.59"
],
"open": 0,
"tags": ""
}
You're writing each row as a separate JSON object one by one, which results in multiple independent objects instead of one combined dictionary. To fix this, you should first collect all the rows into a single dictionary and then write them to the file in one go.
Here's a cleaned-up version of your function:
import json
def save():
all_data = {}
j = 0
for row_id in Trw.get_children():
row_values = Trw.item(row_id)['values']
all_data[str(j)] = {
"text": str(j + 1),
"image": "",
"values": row_values,
"open": 0,
"tags": ""
}
j += 1
with open("vrhsp.json", "w", encoding="utf-8") as outfile:
json.dump(all_data, outfile, indent=4, ensure_ascii=False)
This way, everything gets dumped into the .json
file as one dictionary, and commas between entries will appear correctly.