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


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