python - 如何在python中处理文本文件
问题描述
我有一个文件名,称为words.txt
其中包含字典单词。我调用这个文件并要求用户输入一个单词。然后尝试找出这个词是否存在于这个文件中,如果是,则打印True
其他Word not found
。
wordByuser = input("Type a Word:")
file = open('words.txt', 'r')
if wordByuser in file: #or if wordByuser==file:
print("true")
else:
print("No word found")
words.txt 文件在一行中包含每个字母,然后在第二行中包含新字母。
解决方案
使用这一行解决方案:
lines = file.read().splitlines()
if wordByuser in lines:
....
推荐阅读
- windows - 如何在智能卡凭据提供程序中为域帐户序列化凭据以进行登录和解锁?
- python - 如何将 find_one() 函数与 _id 键一起使用?
- java - Spring HATEOAS RepresentationModel,无法设置属性链接,因为没有设置器,没有枯萎,它不是持久性构造函数的一部分
- python - 如何使用 python 保存和关闭 AutoCAD 应用程序?
- javascript - 在 for 循环中处理多个异步调用
- c - 如何访问结构数组
- python - 为什么这个简单的 Python 代码会因为大数字而崩溃?
- java - 如何使用 Firebase Auth 验证受密码保护的 REST API?
- python - 字典中的双重索引
- python - Matplotlib:如何在散点图上绘制时间序列