Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
2.7.5 Assistance
#1
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:

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.
Reply


Messages In This Thread
2.7.5 Assistance - by patrickperea - Jun-27-2024, 09:25 PM
RE: 2.7.5 Assistance - by deanhystad - Jun-28-2024, 01:13 AM
RE: 2.7.5 Assistance - by patrickperea - Jun-28-2024, 01:34 PM
RE: 2.7.5 Assistance - by deanhystad - Jun-28-2024, 04:49 PM
RE: 2.7.5 Assistance - by deanhystad - Jun-28-2024, 04:59 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020