Python中文
首页
教程
问答
标签
搜索
登录
注册
如何在一行代码中包含.isupper()和.islower()方法?
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>处理一些读取csv文件并获取用户查找的搜索词计数的代码。这个函数不应该区分大小写,因此如果用户想找到单词:trampoline,它会捕获trampoline、trampoline等</p> <p>我想知道是否可以在同一个参数中添加.isupper()和.islower(),以简化代码?我感觉到我在做别的错事,只是很难找出那是什么。你知道吗</p> <p>例如,从.csv文件</p> <pre><code>I have 12 trampolines. The TRAMPOLINES are round and have netting surrounding them. Trampolines are my favorite activity. </code></pre> <p>我尝试在单独的代码行中添加这两个方法,但遇到了意外的输出。你知道吗</p> <pre><code>def countingWords(word): openFile= open('file.csv', 'r') contents = openFile.read() openFile.close counter = 0 for separateLines in contents.split(): if str(word) in separateLines: counter += 1 elif str(word).isupper(): counter += 1 elif str(word).islower(): counter += 1 return count </code></pre> <p>当前,如果用户输入:countingWords('Trampoline'),则输出将仅为1,而应为3</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>将目标词和文本行从文件转换为小写:</p> <pre><code>for separateLines in contents.split(): if word.lower() in separateLines.lower(): counter += 1 </code></pre>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
无法使用Django/mongoengine连接到MongoDB(身份验证失败)
7 回答
无法使用Django\u mssql\u后端迁移到外部hos
5 回答
无法使用Django&Python3.4连接到MySql
1 回答
无法使用Django+nginx上载媒体文件
5 回答
无法使用Django1.6导入名称模式
3 回答
无法使用Django1.7和mongodb登录管理站点
7 回答
无法使用Djangoadmin创建项目,进程使用了错误的路径,因为我事先安装了错误的Python
1 回答
无法使用Djangockedi验证CBV中的字段
5 回答
无法使用Djangocketditor上载图像(错误400)
5 回答
无法使用Djangocron进行函数调用
7 回答
无法使用Djangofiler djang上载文件
3 回答
无法使用Djangokronos
1 回答
无法使用Djangomssql provid
1 回答
无法使用Djangomssql连接到带有Django 1.11的MS SQL Server 2016
9 回答
无法使用Djangomssq迁移Django数据库
4 回答
无法使用Djangonox创建用户
1 回答
无法使用Djangopyodb从Django查询SQL Server
9 回答
无法使用Djangopython3ldap连接到ldap
4 回答
无法使用Djangoredis连接到redis
9 回答
无法使用Django中的FK创建新表
7 回答