How to read a file without newlines?

前端 未结 9 1436
广开言路
广开言路 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:08

    another example:

    Reading file one row at the time. Removing unwanted chars with from end of the string str.rstrip(chars)

    with open(filename, 'r') as fileobj:
        for row in fileobj:
            print( row.rstrip('\n') )
    

    see also str.strip([chars]) and str.lstrip([chars])

    (python >= 2.0)

提交回复
热议问题