Home > front end >  Run a python function for each line in a text file
Run a python function for each line in a text file

Time:03-01

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)

CodePudding user response:

you can do this in this way:

# Read in the file
with open('input.txt', 'r') as file :
  filedata = file.read()

# Replace the target string
filedata = filedata.replace(',', ':')

# Write the file out again
with open('output.txt', 'w') as file:
  file.write(filedata)

CodePudding user response:

You can separate the file contents per line and then apply the text processing functions on each line. After that just append the lines to the response output. The code would be

with open("input") as core:
    a = core.read()

K = '!'
N = 12
a = a.split("\n")
res = ''

for line in a:
  temp = ''
  for idx, ele in enumerate(line):
      if idx % N == 0 and idx != 0:
          temp = temp   K
      else:
          temp = temp    ele
  temp = (str(temp).replace(",",":").replace("!",","))
  res = res temp[:-1] "}\n"
res = res[:-1]
print(res)

For the following input

{'000','001','002','003','004','005','006','007','008','009'}
{'000','001','002','003','004','005','006','007','008','009'}

the output would be:

{'000':'001','002':'003','004':'005','006':'007','008':'009'}
{'000':'001','002':'003','004':'005','006':'007','008':'009'}

CodePudding user response:

I think I would base an answer off of turning your input data into a generator so that I could apply next() to it to take two items at a time.

def clean_line(line):
    items = iter(line.split(","))
    return ','.join(f'{item}:{next(items)}' for item in items)

With a method like clean_line() you might now use:

data = [
    "{'000','001','002','003','004','005','006','007','008','009'}",
    "{'000','001','002','003','004','005','006','007','008','009'}"
]
results = "\n".join(clean_line(line) for line in data)
print(results)

Or reading from a file as:

def clean_line(line):
    items = iter(line.strip("\n").split(","))
    return ','.join(f'{item}:{next(items)}' for item in items)

with open("data.txt", "r") as file_in:
    results = "\n".join(clean_line(line) for line in file_in.readlines())
print(results)
  • Related