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
#2
It "defaults" to any file not named "Nfile.txt". And the match has to be exact, so "nfile.txt" will not add the "N" suffix to the end of the lines.
Reply
#3
(Jun-28-2024, 01:13 AM)deanhystad Wrote: It "defaults" to any file not named "Nfile.txt". And the match has to be exact, so "nfile.txt" will not add the "N" suffix to the end of the lines.

I input the argument of Nfile.txt and it still doesn't add the "suffix" if I do f strings it works but the server doesn't accept f strings because of the python version.
Reply
#4
It is not because of the format string. Your format string is fine. You can check the formatting to verify.
line = "This is the line"
suffix = "N"
desired_length = 60
formatted = "{line:{desired_length}}{suffix}".format(line=line, desired_length=desired_length, suffix=suffix)
print("1234567890" * 6)
print(formatted)
Output:
123456789012345678901234567890123456789012345678901234567890 This is the line N
If you are having problem with the format (which I seriously doubt) use str.ljust(). I checked, and str.ljust has been around since before 2.7.5.
line = "This is the line"
suffix = "N"
desired_length = 60
formatted = line.ljust(desired_length) + suffix
print("1234567890" * 6)
print(formatted)
Add a print to verify that the problem is the if statement and not the format statement.
def reformat_file(filename = filename, desired_length = desired_length, suffix = suffix):
  if filename == WF:
    print("Appending N")
    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 you see "Appending N" printed out, but lines in the file don't end with N, the problem is the format statement. I don't think you will see "Appending N" printed.
What is this supposed to do?
len(line) < desired_length
Was that supposed to clip line to the desired length? It does not do that..

And you have too many loops in your reformat_file function. The function should not be testing the filename and running one of two loops. Code that calls the function should test the filename and pass an approprate suffix. Like this:
def reformat_file(filename, length=87, suffix=""):
    lines = []
    with open(filename, "r") as fp:
        for line in fp:
            lines.append(line[:length].strip().ljust(length) + suffix)

    with open(filename, "w") as fp:
        fp.write("\n".join(lines))


if __name__ == "__main__":
    import sys

    filename = sys.argv[1]
    suffix = "N" if filename == "Nfile.txt" else ""
    reformat_file(filename, suffix=suffix)
Reply
#5
I just noticed this:
Quote: 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)
Your code does not do this.

Your code needs to pad the line to the desired length minus the suffix.
def reformat_file(filename, length=88, suffix=""):
    length = length - len(suffix)
    lines = []
    with open(filename, "r") as fp:
        for line in fp:
            lines.append(line[:length].strip().ljust(length) + suffix)
 
    with open(filename, "w") as fp:
        fp.write("\n".join(lines))
 
 
if __name__ == "__main__":
    import sys
 
    filename = sys.argv[1]
    suffix = "N" if filename == "Nfile.txt" else ""
    reformat_file(filename, suffix=suffix)
Reply


Forum Jump:

User Panel Messages

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