读取 .txt 中全部大写或全部小写的所有行 Python 2.7.1?

发布于 2024-12-20 09:01:48 字数 819 浏览 7 评论 0原文

我正在尝试读取文件中全部大写或全部小写的所有行。

如果 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

七度光 2024-12-27 09:01:48

您收到错误是因为 j 是一个字符串,而不是一个整数(顺便说一下,您不必调用 str(j);它已经是一个字符串)。

您可以删除大小写混合的行,如下所示:

all_one_case = [ line
                 for line in f.readlines()
                 if line.isupper() or line.islower() ]

注意:感谢使用 isupper()islower() (原始使用 < code>re.) 转到这个问题的其他一些答案。

这还包括,例如,10 green Bottles,因为它只包含小写字母,尽管它也包含数字和空格。从这个问题来看,我无法判断这是否是故意的。如果你只想要字母,你可以使用这个测试:

               … if re.match('[A-Z]*$|[a-z]*

如果你想用这些行替换文件,你可以重新打开它进行写入:

with open(file_name, 'r') as f:
    for line in all_one_case:
        f.write(line)
, line) ]

如果你想用这些行替换文件,你可以重新打开它进行写入:

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:

all_one_case = [ line
                 for line in f.readlines()
                 if line.isupper() or line.islower() ]

Note: Credit for the use of isupper() and islower() (The original used re.) 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 re.match('[A-Z]*$|[a-z]*

If you want to replace the file with these lines, you can reopen it for writing:

with open(file_name, 'r') as f:
    for line in all_one_case:
        f.write(line)
, line) ]

If you want to replace the file with these lines, you can reopen it for writing:

随梦而飞# 2024-12-27 09:01:48
def homogeneous_lines (file_name):
    with open(file_name) as fd:
        for line in fd:
            if line.islower() or line.isupper():
                yield line

该函数读取文件中的每一行。然后对每一行检查该行是否全部是大写或小写。

最后我们产生了这条线。

编辑 - 更改为合并 using with 语句,对字符串使用内置 islower() 和 isupper() ,并制作成生成器。

def homogeneous_lines (file_name):
    with open(file_name) as fd:
        for line in fd:
            if line.islower() or line.isupper():
                yield line

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.

冷血 2024-12-27 09:01:48

如果在一行中只能有一个符号,那么我不知道为什么要将行 (k) 转换为元组,而对 f.readlines() 的第二次调用是可能是错误。第一次调用 f.readlines() 后,所有行都在 lines 变量中,并且在循环中您可以逐行检查它。

如果你想检查整个字符串是小写还是大写,那么使用这样的代码:

if line.islower() or line.isupper():
    print(line)

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 to f.readlines() is probably bug. After 1st call of f.readlines() you have all lines in lines 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:

if line.islower() or line.isupper():
    print(line)
居里长安 2024-12-27 09:01:48
f=open(file_name,'r')
print [l for l in f.readlines() if l.islower() or l.isupper()]
f=open(file_name,'r')
print [l for l in f.readlines() if l.islower() or l.isupper()]
凡间太子 2024-12-27 09:01:48

使用 with 语句打开文件。这样您就可以放心,即使出现异常,文件也会关闭
使用字符串方法 islowerisupper 来检查字符串是全大写还是小写。例如这样:

with open(filename) as f:
  output = [line for line in f if line.isupper() or line.islower()]

use the with statement for open the file. This way you are be safe that the file will get closed even in case of an exception.
Use the string methods islower and isupper to check whether the string is all upper or lower case. For example like this:

with open(filename) as f:
  output = [line for line in f if line.isupper() or line.islower()]
猛虎独行 2024-12-27 09:01:48

我会使用列表理解:

f=open(file_name,'r')

lines = f.readlines()

ul_lines = [line.rstrip('\n') for line in lines if line.islower() or line.isupper()] 

如果你的文件是 unicode,你应该调整它,但这是一般的想法。

rstrip部分是去掉末尾的'\n'。

另外,内存效率更高的版本

f=open(file_name,'r')

ul_lines = [line.rstrip('\n') for line in f if line.islower() or line.isupper()] 

您必须重新打开文件才能重复它。

如果您的内存非常有限,您应该使用生成器表达式:

f=open(file_name,'r')

ul_lines_gen = (line.rstrip('\n') for line in f if line.islower() or line.isupper())

如果您只想要字符串中的字母而不需要数字,请添加 line.rstrip('\n').isalpha() 条件。

I'd use list comprehensions:

f=open(file_name,'r')

lines = f.readlines()

ul_lines = [line.rstrip('\n') for line in lines if line.islower() or line.isupper()] 

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

f=open(file_name,'r')

ul_lines = [line.rstrip('\n') for line in f if line.islower() or line.isupper()] 

You'll have to reopen the file to repeat it though.

If you are very memory constrained you should use a generator expression:

f=open(file_name,'r')

ul_lines_gen = (line.rstrip('\n') for line in f if line.islower() or line.isupper())

And if you want only letters and no digits in your strings add line.rstrip('\n').isalpha() condition.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文