> For the complete documentation index, see [llms.txt](https://close.gitbook.io/yun-wei-bi-ji/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://close.gitbook.io/yun-wei-bi-ji/python/python-xiao-ji-qiao/python-du-qu-wen-jian-qu-chu-hui-che.md).

# Python读取文件去除回车

```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']
```
