![]() |
2.7.5 Assistance - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: 2.7.5 Assistance (/thread-42383.html) |
2.7.5 Assistance - patrickperea - Jun-27-2024 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. RE: 2.7.5 Assistance - deanhystad - Jun-28-2024 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. RE: 2.7.5 Assistance - patrickperea - Jun-28-2024 (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. RE: 2.7.5 Assistance - deanhystad - Jun-28-2024 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) 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)) returnIf 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_lengthWas 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) RE: 2.7.5 Assistance - deanhystad - Jun-28-2024 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) |