Jun-27-2024, 09:25 PM
Hello I am in need of some assistance, to quickly sum it up I obtain an incoming payment file that is 33 characters long on each line and I need it to be 88 characters on each line with one of the files ending with N (get 4 files in total a day). I wrote the script with no issue with 3.10 using f string but found out the server is 2.7.5 so had to change it to .format instead. The issue that I am running into is the file that is supposed to have the N does not format during conversion but will space it out.
Desired output
AAA N
AAA N
Output I am getting is
AAA
AAA
My code:
I dont know why it defaults to the else in my if statement, any help would be appreciated.
Desired output
AAA N
AAA N
Output I am getting is
AAA
AAA
My code:
import sys filename=sys.argv[1] WF = 'Nfile.txt' suffix='N' desired_length=87 lines = [] def reformat_file(filename = filename, desired_length = desired_length, suffix = suffix): if filename == WF: with open(filename, 'r') as fp: for line in fp: line = line.strip() formatted = line len(line) < desired_length formatted = "{line:{desired_length}}{suffix}".format(line=line, desired_length=desired_length, suffix=suffix) lines.append(formatted) return else: with open(filename, 'r') as fp: for line in fp: line = line.strip() formatted = line len(line) < desired_length formatted = "{line:{desired_length}}".format(line=line, desired_length=desired_length) lines.append(formatted) with open(filename, 'w') as fp: fp.write('\n'.join(lines)) return if __name__ == "__main__": reformat_file(filename)
I dont know why it defaults to the else in my if statement, any help would be appreciated.