need a bit of help here
I have a python script that copies and edits over txt files
but I'm having an issue with the indent
and I am not sure how to repair it
The indent I normally use is indent=4
and for the most part it has never failed
Now I am working on smaller files and the files have different indents so when it copies over the file for editing it removed 2 rows
the original file starts with 6 indents and increases and decreases as it continues
Here is the original look
{
......"unid": 100, - indent 6
"awar": false,
"upst": [
........{ - indent 8
.........."lvls": [ - indent 10
............{ - indent 12
.............."ulpf": true, - indent 14
"fsg": [
0,
0,
0,
0,
0
]
}
]
}
],
"nupl": {
"txt": "",
"cpf": true
},
"esup": false,
"elcl": 0,
"gacp": false,
"paid": 5214,
"lzsid": 24434,
"livid": 214867,
"isra": false
}
Here is the Results
Top of Market
{
...."unid": 1, - indent 4
"awar": false,
"upst": [
........{ - indent 8
............"lvls": [ - indent 12
................{
"ulpf": true, - indent 16
"fsg": [
0,
0,
0,
0,
0
]
}
]
}
],
"nupl": {
"txt": "Marks1",
"cpf": true
},
"esup": false,
"elcl": 0,
"gacp": false,
"paid": 125,
"lzsid": 24434,
"livid": 214867,
"isra": false
},
Bottom of Market
So when I increase the indent to 6 it places all the codes in the right spots but now this section also increase by 2
this is the result with 6
Top of Market
{
"unid": 1,
"awar": false,
"upst": [
{
"lvls": [
{
"ulpf": true, --- as you can see here it's moved over a ton
"fsg": [ moved over 24 indents
0,
0,
0,
0,
0
]
}
]
}
],
"nupl": {
"txt": "Marks1",
"cpf": true
},
"esup": false,
"elcl": 0,
"gacp": false,
"paid": 125,
"lzsid": 24434,
"livid": 214867,
"isra": false
},
Bottom of Market
what is the best way to fix this problem
Here is the script
import shutil
import glob
import json
import re
import asyncio
import sys
from asyncore import loop
unit_to_paid = {"(1)": 125,
"(2)": 124}
async def update_file(file, arg):
f = open(file, "r")
text = f.read()
data = json.loads(text)
num = re.search(r"\(([^()] )\)", file)
data["unid"] = int(num.group(1))
data["paid"] = unit_to_paid[num.group(0)]
data["nupl"]["txt"] = f'Marks{num.group(1)}'
f.close()
f = open(file, "w")
f.truncate(0)
json.dump(data, f, indent=4) ------ here is the indent
f.write(", \nBottom of Market")
f.close()
f1 = open(file, "r ")
content = f1.read()
f1.seek(0, 0)
f1.write("Top of Market" '\n' content)
f1.close()
async def get_update_files(arg):
# Recursively grabs the files
files = glob.glob('New folder 2/**/*.txt', recursive=True)
# Creates a list of tasks to run concurrently
tasks = []
for file in files:
tasks.append(asyncio.create_task(update_file(file, arg)))
t = asyncio.gather(*tasks)
return t
def main():
args = sys.argv[1:]
# Deletes New folder 2 if it exists
shutil.rmtree('New folder 2', ignore_errors=True)
# Copies existing files into New folder 2
shutil.copytree('New folder 1', 'New folder 2')
# Will get each file and update them concurrently
if len(args) == 0:
asyncio.get_event_loop().run_until_complete(get_update_files("all"))
else:
asyncio.get_event_loop().run_until_complete(get_update_files(args[0]))
main()
I have tried a few things but nothing works I even removed this section so to edit the originals
# Deletes New folder 2 if it exists
shutil.rmtree('New folder 2', ignore_errors=True)
# Copies existing files into New folder 2
shutil.copytree('New folder 1', 'New folder 2')
and updated this files = glob.glob('New folder 1/**/*.txt', recursive=True)
CodePudding user response:
Python's standard json
module, like most JSON encoders, doesn't offer fine control over whitespace. If you need that, then you probably can't use json.dump
, at least for the entirety of data
. Instead, write your own code to produce the JSON you want, falling back on json.dump
for subcomponents when you can.
CodePudding user response:
Maybe I didn't express myself clear enough in the comment. My idea is first to generate the file with the indent=2, it will give the result:
{
.."unid": 100, - indent 2
"awar": false,
"upst": [
....{ - indent 4
......"lvls": [ - indent 6
........{ - indent 8
.........."ulpf": true, - indent 10
"fsg": [
0,
And after that you add 4 more spaces to all the lines which already start with a space (it excludes lines containing {
and }
at the start, so they are not shifted):
{
......"unid": 100, - indent 2 4 = 6
"awar": false,
"upst": [
........{ - indent 4 4 = 8
.........."lvls": [ - indent 6 4 = 10
............{ - indent 8 4 = 12
.............."ulpf": true, - indent 10 4 = 14
"fsg": [
0,