How to read a file without newlines?

前端 未结 9 1426
广开言路
广开言路 2020-11-21 21:03

In Python, calling

temp = open(filename,\'r\').readlines()

results in a list in which each element is a line in the file. It\'s a little st

相关标签:
9条回答
  • 2020-11-21 21:19
    my_file = open("first_file.txt", "r")
    for line in my_file.readlines():
        if line[-1:] == "\n":
            print(line[:-1])
        else:
            print(line)
    my_file.close() 
    
    0 讨论(0)
  • 2020-11-21 21:26
    temp = open(filename,'r').read().split('\n')
    
    0 讨论(0)
  • 2020-11-21 21:33

    Try this:

    u=open("url.txt","r")  
    url=u.read().replace('\n','')  
    print(url)  
    
    0 讨论(0)
提交回复
热议问题