Python读取文件去除回车

# 方法1: 基于list的索引操作
with open("demo.txt", "r") as f:
    data = f.readlines()
    print(data)
    a = data[0][:-1]
    b = data[1]
    print(a, b)

output[1]:
['35durant\n', 'teamGSW']
35durant teamGSW


# 方法2: 基于str的splitlines()方法
with open("demo.txt", "r") as f:
    data = f.read().splitlines()
    print(data)

output[1]: 
['35durant', 'teamGSW']

Last updated