简体   繁体   中英

Python Program to Read in a File of Text and then Write its Contents in Another File, with Margins of Different Sizes, Input By User

I am writing a new Python code and want to make it read in a file, then re-write the contents of the file to another one, but with added margins. The margins have to be input by the user and the text has to be left aligned.

This is for a Python project, 3.74 version. I already successfully wrote the code for copying the file from the old one, as well as creating the left margins, but I am having a hard time in finding a good way to create the right margins. Also, I need to determine when and where the line needs to be split and go to the next file. Words cannot be split.

#ask user to input file Name
print("\n\nEnter your file's name")

#have a file name ready
file_Name = "I'm a file.txt" 

#set the line length in characters
line_Size=80
#get min size
min_Size = 7

#take in user input
#make file_Name be the name input by user
file_Name=input()

#output file 
#get name from user
print("Enter the name of the file you want to output to (i.e. output file)")
show = input()

#get margins from User
#set default margins first
left_Margin=0
right_Margin=0

#ask for left margin 
print("\nEnter your left margin")
#get left margin
left_Margin=int(input())

#ask for right left_Margin
print("\nEnter your right margin")
#get right margin 
right_Margin=int(input())
#print margins testcase
#print(left_Margin, right_Margin)

#create varible to hold the number of characters to withold from line_Size
avoid = right_Margin
num_chars = left_Margin
#open file now


with open (file_Name, "r") as f:
    #get output file ready 
  with open(show, "w") as f1:

    for i in f:
      num_chars += len(i)
      string_length=len(i)+left_Margin
      #string_squeeze=len(i)+right_Margin
      i=i.rjust(string_length)
      words = i.split()
      #check if num of characters is enough
      if num_chars-80-avoid-5<min_Size: 
        print("Here is the problem")
        f1.write(i)
        i.split()
        f1.write('\n')
        f1.write(i)

      else:
        f1.write(i)
        f1.write("\n")

You created words from i.split() and did nothing with it. From there it's hard to say where your mind went but it's what my code looks like when I bamboozle myself. I would handle it like this:

leftmargin = leftmargin - 1 # we will add a space before the first word of each line

outline = " " * leftmargin  # line to write
for inline in file:
    for word in inline.split():
        if len(outline) + len(word) + rightmargin > max:
            # line would be too long, so write what we have and reset the outline variable
            outfile.write(outline)
            outline = " " * leftmargin
        # the above flow nicely prevents the need for more if/elses
        outline += ' ' + word

this could be improved to handle 0 leftmargins and words longer than the max line length, but I think it should hold you for now.

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