Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何打印文件中的所有大写字符串_Python_Python 3.x - Fatal编程技术网

Python 如何打印文件中的所有大写字符串

Python 如何打印文件中的所有大写字符串,python,python-3.x,Python,Python 3.x,我要讨论的问题是,它没有打印出任何内容 我的if语句有问题吗?因为它打印出了整个文本。当我没有添加if语句时 fname = input('Enter the file name: ') try: fhand = open(fname) except: print('File cannot be opened:', fname) exit() for line in fhand: line = line.strip() if line.is

我要讨论的问题是,它没有打印出任何内容

我的if语句有问题吗?因为它打印出了整个文本。当我没有添加if语句时

fname = input('Enter the file name: ')

try:
    fhand = open(fname)
except:
       print('File cannot be opened:', fname)
       exit()

for line in fhand:
    line = line.strip()
    if line.isupper():
        print(line)

该文件包含HTML、CSS、I/O和SQL等内容,并且应该打印出所有这些字符串

要检查是否相等,请使用
=
。但是,我很确定您是在单独查找
isupper
,而不是检查它是否相等
isupper
返回一个布尔值,可通过
if
语句进行检查:

for line in fhand:
    line = line.strip()
    if line.isupper():
        print(line)

您的
if
语句使用的
=
用于赋值,而不是用于比较。而是使用
==
。除此之外,
string.isupper()
本身是一个布尔值,因此您将把字符串与布尔值进行比较,这将不会产生您想要的结果。

您可以使用
过滤器
函数:

for line in filter(str.isupper, fhand):
    print(line)

使用with语句而不是try/except打开文件可能更为惯用,因为一旦对象到达作用域的末尾,try/except将处理关闭文件的操作。

--更新--

从上面的链接中,可以看到投票否决该答案的个人:

处理文件时,最好使用with关键字 物体。这样做的好处是,文件在运行后会正确关闭 套件完成,即使在某个点出现异常。使用 with也比编写等效try finally块短得多

这是完全正确的

返回line对象的副本,该对象的类型为str(string),删除了空格,并在该对象上调用该对象返回布尔值

范例

line = '  AES '
print(line.strip().isupper())
将返回真实和

line = ' aes '
print(line.strip().isupper())
将返回False

--更新--

完整性请参见@Tomothy32注释

import os

path_to_file = "/path/to/file"

if os.path.isfile(path_to_file):
    with open(path_to_file, 'r') as fileobj:
        for line in fileobj:
            if line.strip().isupper():
                print(line)
else:
    print("Please pass valid path to file")
    exit()

如果line=line.isupper()
正在为
line
赋值,没有检查它是否都是大写的(与
=
=
之间的区别)。我现在发现了我的问题,open()不允许我读取()文件,我需要read()方法才能对文件进行操作。谢谢你的评论,这是一个错误。我不是有意在那里输入的。不是我,但我猜这是因为你说
with
try
-
的直接替代品,但它不是。(
with
try
-
finally
)此处的
try
-
用于检查文件是否存在。谢谢,这是可以理解的,我将确保进行完整的检查:)我认为path\u to\u文件足以表明路径验证已经/应该在with/open组合之前完成:/
line = ' aes '
print(line.strip().isupper())
import os

path_to_file = "/path/to/file"

if os.path.isfile(path_to_file):
    with open(path_to_file, 'r') as fileobj:
        for line in fileobj:
            if line.strip().isupper():
                print(line)
else:
    print("Please pass valid path to file")
    exit()