Ok so I am trying to mass format a large text document to convert
#{'000','001','002','003','004','005','006','007','008','009'}
into
#{'000':'001','002':'003','004':'005','006':'007','008':'009'}
using python and have my function working, however it will only work if I run it line by line.
and was wondering how to get it to run for each line on my input so that it will work on a multi line document
with open("input") as core:
a = core.read()
K = '!'
N = 12
res = ''
for idx, ele in enumerate(a):
if idx % N == 0 and idx != 0:
res = res + K
else:
res = res + ele
b = (str(res).replace(",",":").replace("!",","))
l = len(b)
c = b[:l-1]
d = c + "}"
print(d)
here is the current result for a multiline text file
{'000':'001','002':'003','004':'005','006':'007','008':'009',
{'001':'00,':'003':'00,':'005':'00,':'007':'00,':'009':'00,'}
{'002':',03':'004':',05':'006':',07':'008':',09':'000':',01'}
{'003','004':'005','006':'007','008':'009','000':'001','002'}
So Far I have tried
with open('input', "r") as a:
for line in a:
K = '!'
N = 12
res = ''
for idx, ele in enumerate(a):
if idx % N == 0 and idx != 0:
res = res + K
else:
res = res + ele
b = (str(res))
l = len(b)
c = b[:l-1]
d = c + "}"
print(d)
but no luck
FOUND A SOLUTION
import re
with open("input") as core:
coords = core.read()
sword = coords.replace("\n",",\n")
dung = re.sub('(,[^,]*),', r'\1 ', sword).replace(",",":").replace(" ",",").replace(",\n","\n")
print(dung)
I know my solution works, but i cant quite apply this to other situations where I am applying different formats based on the need. Its easy enough to work out how to format a single line of text as there is so much documentation out there.
Does anybody know of any plugins or particular python elements where you can write your format function and then apply it to all lines. like a kind of applylines() extension instead of readlines()