python 去掉\N
时间: 2023-07-22 10:10:01 浏览: 183
python按行读取文件,去掉每行的换行符\n的实例
可以使用 `.strip()` 方法去掉每行末尾的 '\n'。例如:
```
with open("file.txt", "r") as f:
lines = f.readlines()
for line in lines:
line = line.strip()
print(line)
```
也可以使用列表推导式:
```
with open("file.txt", "r") as f:
lines = f.readlines()
lines = [line.strip() for line in lines]
print(lines)
```
阅读全文