Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.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/16.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,我在文件中有几个字符串,如下所示: line1 <img alt="Powered by MediaWiki" height="31" src="/static/images/poweredby_mediawiki_88x31.png" srcset="/static/images/poweredby_mediawiki_132x47.png 1.5x, /static/images/poweredby_mediawiki_176x62.png 2x" width="88"/>

我在文件中有几个字符串,如下所示:

line1    <img alt="Powered by MediaWiki" height="31" src="/static/images/poweredby_mediawiki_88x31.png" srcset="/static/images/poweredby_mediawiki_132x47.png 1.5x, /static/images/poweredby_mediawiki_176x62.png 2x" width="88"/>
line2    '<img alt="" class="wp-image-141 size-large" height="591" sizes="(max-width: 788px) 100vw, 788px" src="https://alessandrorossini.org/wp-content/2018/07/20180619_151349-1024x768.jpg" srcset="https://alessandrorossini.org/wp-content/2018/07/20180619_151349-1024x768.jpg 1024w, https://alessandrorossini.org/wp-content/2018/07/20180619_151349-300x225.jpg 300w, https://alessandrorossini.org/wp-content/2018/07/20180619_151349-788x591.jpg 788w" width="788"/>
line1
第2行'
我想读取每行中的高度值(例如:第1行中的31和第2行中的591)


如何执行此操作?

为了运行下面的代码,我将您的两行代码放入一个名为
file\u name.html
的文件中。这里有两种提取高度值的方法

与BeautifulSoup一起使用

from bs4 import BeautifulSoup

with open('file_name.html', 'r') as f:
    soup = BeautifulSoup(f, 'html5lib')
    for img_tag in soup.find_all('img'):
        print(img_tag.get('height'))
使用正则表达式

import re

with open('file_name.html', 'r') as f:
    lines = f.readlines()
    regex = '(height=")(\d*)(")'  # 2nd regex group captures the value of height
    heights = [re.search(regex, l).group(2) for l in lines]
    print(heights)

请注意,这个特定的正则表达式示例只捕获每行的第一个
高度值

到目前为止你试过什么吗?那是什么?为什么不起作用?无论哪种方法,都可以查看Python BeautifulSoup。各种方法可以获得您想要的东西。这里有一个需要研究的问题:看起来您正在尝试解析html,例如,在这种情况下,我建议使用合适的html解析器。如果这还不够,我建议调查一下