读取 .txt 中全部大写或全部小写的所有行 Python 2.7.1?
我正在尝试读取文件中全部大写或全部小写的所有行。
如果 file.txt
包含:
Rememberr 8? when you
. Tellingmee THAT one
didntrememberthat
onethingtoday
我希望它读取:
didntrememberthat
ONETHINGTODAY
到目前为止我有:
def function(file_name):
data=[]
f=open(file_name,'r')
lines=f.readlines()
for k in lines:
single=tuple(k)
for j in single:
j=str(j)
if j.isupper() or j.islower() == False:
lines=f.readlines()[j:]
然后我得到这个:
lines=f.readlines()[j:]
TypeError: slice indices must be integers or None or have an __index__ method
这是有道理的,因为 j
不是整数。但是,当遇到 if
语句时,如何找到 j
的位置呢?
如果有一种更简单的方法来做到这一点那就太棒了
I'm trying to read all lines of a file that are either all upper or all lower case.
If file.txt
contains:
Rememberr 8? when you
. Tellingmee THAT one
didntrememberthat
onethingtoday
I would want it to read:
didntrememberthat
ONETHINGTODAY
So far I have:
def function(file_name):
data=[]
f=open(file_name,'r')
lines=f.readlines()
for k in lines:
single=tuple(k)
for j in single:
j=str(j)
if j.isupper() or j.islower() == False:
lines=f.readlines()[j:]
Then i get this:
lines=f.readlines()[j:]
TypeError: slice indices must be integers or None or have an __index__ method
which makes sense because j
isn't an integer. But how would i find the position of j
when it encounters the if
-statement?
If there is an easier way to do it that would be awesome
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您收到错误是因为 j 是一个字符串,而不是一个整数(顺便说一下,您不必调用
str(j)
;它已经是一个字符串)。您可以删除大小写混合的行,如下所示:
注意:感谢使用
isupper()
和islower()
(原始使用 < code>re.) 转到这个问题的其他一些答案。这还包括,例如,
10 green Bottles
,因为它只包含小写字母,尽管它也包含数字和空格。从这个问题来看,我无法判断这是否是故意的。如果你只想要字母,你可以使用这个测试:如果你想用这些行替换文件,你可以重新打开它进行写入:
You are getting the error because j is a string, not an integer (you don't have to call
str(j)
, by the way; it's already a string).You can remove lines with a mix of upper- and lower-case like this:
Note: Credit for the use of
isupper()
andislower()
(The original usedre
.) goes to some other answers to this question.This will also include, e.g.,
10 green bottles
, since it only contains lowercase letters, even though it also contains digits and spaces. From the question, I can't tell whether this is the intent or not. If you want only letters, you can use this test instead:If you want to replace the file with these lines, you can reopen it for writing:
该函数读取文件中的每一行。然后对每一行检查该行是否全部是大写或小写。
最后我们产生了这条线。
编辑 - 更改为合并 using with 语句,对字符串使用内置 islower() 和 isupper() ,并制作成生成器。
This function reads through every line in the file. Then for every line checks to see if the line is all upper or lower case.
Finally we yield the line.
EDIT - Changed to incorporate using with statement, use builtin islower() and isupper() for strings, and made into a generator.
如果在一行中只能有一个符号,那么我不知道为什么要将行 (
k
) 转换为元组,而对f.readlines()
的第二次调用是可能是错误。第一次调用f.readlines()
后,所有行都在lines
变量中,并且在循环中您可以逐行检查它。如果你想检查整个字符串是小写还是大写,那么使用这样的代码:
If in one line you can have only one symbol then I do not know why do you convert line (
k
) into tuple, and 2nd call tof.readlines()
is probably bug. After 1st call off.readlines()
you have all lines inlines
variable and in loop you can check it line by line.If you want to check is whole string is lowercase or uppercase, then use such code:
使用
with
语句打开
文件。这样您就可以放心,即使出现异常,文件也会关闭
。使用字符串方法
islower
和isupper
来检查字符串是全大写还是小写。例如这样:use the
with
statement foropen
the file. This way you are be safe that the file will getclose
d even in case of an exception.Use the string methods
islower
andisupper
to check whether the string is all upper or lower case. For example like this:我会使用列表理解:
如果你的文件是 unicode,你应该调整它,但这是一般的想法。
rstrip部分是去掉末尾的'\n'。
另外,内存效率更高的版本
您必须重新打开文件才能重复它。
如果您的内存非常有限,您应该使用生成器表达式:
如果您只想要字符串中的字母而不需要数字,请添加 line.rstrip('\n').isalpha() 条件。
I'd use list comprehensions:
You should tweak it if your file is unicode, but that's the general idea.
The rstrip part is to get rid of '\n' at the end.
Also, more memory efficient version
You'll have to reopen the file to repeat it though.
If you are very memory constrained you should use a generator expression:
And if you want only letters and no digits in your strings add line.rstrip('\n').isalpha() condition.