python - 从列表的每个元素中剥离 \n
问题描述
我目前对 python 完全陌生,并试图构建一个简单的刽子手游戏。我创建了一个包含所有示例单词的 .txt 文件并将其导入 python。然而,当它们打印出来时,它们都具有这种格式: ['exampleword\n'], ['exampleword2\n'] 但是我想摆脱 \n 结尾。我尝试了这个线程的大部分建议:如何从列表中的字符串末尾删除 '\n'?,但他们没有工作。
woerter = open("woerter.txt", "r")
wortliste = [line.split("\t") for line in woerter.readlines()]
print(wortliste)
我有 python 3.8.2。已安装,非常感谢任何帮助:)
解决方案
尝试:
woerter = open("woerter.txt", "r")
wortliste = [line.rstrip() for line in woerter.readlines()]
print(wortliste)
推荐阅读
- firebase - Flutter如何获取firestore子集合ID
- java - 我可以在这里做什么来检测这两种方法中的重复项
- python - 如何使我的数据帧的 Ngram 的字典以一些字符串 Python 开头
- python - Dask 数据框中指定的列太多
- python - 从 URL 到 Python (Pandas) 的压缩文件
- go - 下载 Go 包
- angular - 在 Spring Boot 中将 Angular 中的 put 请求传递给后端时出现错误 400
- javascript - 从 ajax 响应中获取表列平均值
- ruby-on-rails - 模型的 rails 单元测试返回无效结果
- python - Python问题:如何解决while函数中的简单数字?