python 去掉\N
时间: 2023-07-22 11:10:01 浏览: 189
NLP Python
5星 · 资源好评率100%
可以使用 `.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)
```
阅读全文