简体   繁体   中英

How to modify file so that each line is the same number of characters long?

I have a text file that has rows of data. Before I can pass it downstream, I need each line to have the same number of characters.

I have a python script that finds the longest line in the file, and am trying to use the ljust function to make each line that length.

    args=parse_args()
    readfile = args.inputfile

    #find the longest line in the file, and set that as longest
    longest = 0
    #open the file up
    with open(str(args.inputfile).strip('[]').strip("''")) as readfile:
        #find the longest line in the file and make note of how long.
        for line in readfile:
            if len(line) > longest:
                longest = len(line)
            else:
                pass
        print("The longest line is " + str(longest) + " characters long. ")
        #make each line exactly that long
        for line in readfile:
            readfile.write(line.ljust(longest)) #make it longest long and fill with spaces.

        readfile.close()

The problem is that nothing is happening to the file. The script outputs that the longest line is 31 characters long, but adds no spaces to the end of shorter lines like I expect it to.

You exhausted your file iterator; when you try to write, there's nothing left in the file to access. Had you bothered to trace the execution, you would have seen this. See this lovely debug blog for help.

In particular, let's look at your loops.

#open the file up
with open(str(args.inputfile).strip('[]').strip("''")) as readfile:
    #find the longest line in the file and make note of how long.
    for line in readfile:

This for statement works through the file object's defined iterator; you can think of this as a one-use theme-park ride through the file, set up when you hit the with open statement.

        if len(line) > longest:
            longest = len(line)

I deleted else: pass because it doesn't do anything.

Here, at leaving the for loop, the file descriptor's "bookmark" is at the end of the file.

    print("The longest line is " + str(longest) + " characters long. ")
    #make each line exactly that long

    for line in readfile:

You won't enter this code; the bookmark is already at the end of the code. There isn't anything else to read. You get the EOF response and skip the loop entirely.

        readfile.write(line.ljust(longest)) #make it longest long and fill with spaces.

    readfile.close()

The repair is fairly simple: use the first block only to determine the maximum line length. Exit the with block entirely. Then make a new one exclusively for writing. Note that you need a new output file, or you need to retain the input from the first reading. Your purpose is to overwrite the original file, which means that you can't be reading it at the same time.

If this is still confusing, then please work through a couple of tutorials on file handling.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM