Removal of the area without a new line



  • My code.

    f = open('1.txt', 'r') 
    lines = f.readlines()
    str1= lines[0]
    str2= lines[2]
    print(str1,str2)
    

    His conclusion

    str number 1
     str number 3
    

    There are lines in 1.txt.

    str number 1 
    str number 2
    str number 3
    

    What I want is out.

    str number 1 str number 3

    Thank you.



  • The thing is, the file at the end of each line has a line translation. \nYou need to get rid of it, for example:

    str1 = lines[0][:-1]
    str2 = lines[2][:-1]
    print(str1, str2)
    

    i.e. cut the last symbol (which) \n)

    You can do this for the entire file:

    lines = [line[:-1] for line in f.readlines()]
    

Log in to reply
 

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2