Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.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 - Fatal编程技术网

Python 如何读取只打印包含整数的文本文件行的多行文本文件?

Python 如何读取只打印包含整数的文本文件行的多行文本文件?,python,Python,我想写一个程序,只打印出包含整数的文本文件行 输入示例: This line does not have any integers P3rhaps th3re are some in this one Definitely none in this line This is the 4th line 预期产量 P3rhaps th3re are some in this one This is the 4th line 通过将包含任何数字字符的任何行指定给列表,可以在open()的循环中使用

我想写一个程序,只打印出包含整数的文本文件行

输入示例:

This line does not have any integers
P3rhaps th3re are some in this one
Definitely none in this line
This is the 4th line
预期产量

P3rhaps th3re are some in this one
This is the 4th line

通过将包含任何数字字符的任何行指定给列表,可以在open()的循环中使用列表理解

    with open('a.txt') as f:
        output = [x.strip('\n') for x in f if any(c.isnumeric() for c in x)]

    output
输出

    ['P3rhaps th3re are some in this one', 'This is the 4th line']

你能发布到目前为止你写了什么代码吗?或者失败在哪里?
将open('numbers.txt')作为f:
输出=[x.strip('\n')表示f中的x(如果有)(c.isnumeric()表示x中的c)]
output
我试过了,但是我没有用repl.it将即时消息输出到程序中,当我运行代码时什么也不会显示,它只是空白。Nvm我修复了它,感谢您的帮助,我刚刚添加了打印(output)