Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.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中的文件行获取URL_Python_Regex_Python 2.7_Python 3.x - Fatal编程技术网

从python中的文件行获取URL

从python中的文件行获取URL,python,regex,python-2.7,python-3.x,Python,Regex,Python 2.7,Python 3.x,这是一行文件,我只想在单词uri之后使用url,然后在smallPictureUrl之后使用url,但我找不到合适的方法 星号代表文本或数字,或者两者同时代表,每一行中的星号看起来都不一样,因此它们没有帮助,也没有一种模式可以利用它 {"bigPictureUrl":"https://fbcdn-profile-a.akamaihd.net/*-*-*/*.*.*.*/*/*.jpg", "timelineCoverPhoto":"{\"focus\":{\"x\":0.5,\"y\":0.4

这是一行文件,我只想在单词uri之后使用url,然后在smallPictureUrl之后使用url,但我找不到合适的方法

星号代表文本或数字,或者两者同时代表,每一行中的星号看起来都不一样,因此它们没有帮助,也没有一种模式可以利用它

{"bigPictureUrl":"https://fbcdn-profile-a.akamaihd.net/*-*-*/*.*.*.*/*/*.jpg", 
"timelineCoverPhoto":"{\"focus\":{\"x\":0.5,\"y\":0.49137931034483},\"photo\":{\"__type__

\":{\"name\":\"Photo\"},\"image_lowres\":{\"uri\":\"https://fbcdn-*-*-*.*.*/*-*-*/*.jpg
\",\"width\":180,\"height\":135}}}",
    "subscribeStatus":"IS_SUBSCRIBED","smallPictureUrl":"https://fbcdn-profile-a.akamaihd.net/*-*-*/*.*.*.*/*/*.jpg",
以更简单的方式,例如:

{"displayName":"Jim Test","firstName":"*","lastName":"*"} 

我设法以这个名称为例,使用
re.search(“(?与
timelineCoverPhoto
关联的值似乎是字符串化的JSON,因此您可以做一些公认丑陋的事情,比如:

import json 
s = {
        "subscribeStatus": "IS_SUBSCRIBED",
        "bigPictureUrl": "https://fbcdn-profile-a.akamaihd.net/*-*-*/*.*.*.*/*/*.jpg",
        "timelineCoverPhoto": "{\"focus\":{\"x\":0.5,\"y\":0.49137931034483},\"photo\":{\"__type__\":{\"name\":\"Photo\"},\"image_lowres\":{\"uri\":\"https://fbcdn-*-*-*.*.*/*-*-*/*.jpg \",\"width\":180,\"height\":135}}}",
        "smallPictureUrl": "https://fbcdn-profile-a.akamaihd.net/*-*-*/*.*.*.*/*/*.jpg" 
    } 

j = json.loads(s.get('timelineCoverPhoto')) 
print "uri:", j.get('photo').get('image_lowres').get('uri')

uri: https://fbcdn-*-*-*.*.*/*-*-*/*.jpg 

timelineCoverPhoto
相关的值似乎是字符串化的JSON,因此您可以做一些公认的丑陋的事情,比如:

import json 
s = {
        "subscribeStatus": "IS_SUBSCRIBED",
        "bigPictureUrl": "https://fbcdn-profile-a.akamaihd.net/*-*-*/*.*.*.*/*/*.jpg",
        "timelineCoverPhoto": "{\"focus\":{\"x\":0.5,\"y\":0.49137931034483},\"photo\":{\"__type__\":{\"name\":\"Photo\"},\"image_lowres\":{\"uri\":\"https://fbcdn-*-*-*.*.*/*-*-*/*.jpg \",\"width\":180,\"height\":135}}}",
        "smallPictureUrl": "https://fbcdn-profile-a.akamaihd.net/*-*-*/*.*.*.*/*/*.jpg" 
    } 

j = json.loads(s.get('timelineCoverPhoto')) 
print "uri:", j.get('photo').get('image_lowres').get('uri')

uri: https://fbcdn-*-*-*.*.*/*-*-*/*.jpg 

如果您不同意使用json,那么这个怎么样

>>> print mytext

{"bigPictureUrl":"https://fbcdn-profile-a.akamaihd.net/*-*-*/*.*.*.*/*/*.jpg", 
"timelineCoverPhoto":"{"focus":{"x":0.5,"y":0.49137931034483},"photo":{"__type__

":{"name":"Photo"},"image_lowres":{"uri":"https://fbcdn-*-*-*.*.*/*-*-*/*.jpg
","width":180,"height":135}}}",
    "subscribeStatus":"IS_SUBSCRIBED","smallPictureUrl":"https://fbcdn-profile-a.akamaihd.net/*-*-*/*.*.*.*/*/*.jpg",

>>> uri = re.findall(r'uri\"\:\"[\'"]?([^\'" >]+)', mytext) #gets the uri
>>> smallpicurl = re.findall(r'smallPictureUrl\"\:\"[\'"]?([^\'" >]+)', mytext) # gets the smallPictureUrl
>>> ''.join(uri).rstrip()
'https://fbcdn-*-*-*.*.*/*-*-*/*.jpg' # uri
>>> ''.join(smallpicurl).rstrip()
'https://fbcdn-profile-a.akamaihd.net/*-*-*/*.*.*.*/*/*.jpg' # smallPictureUrl

如果您不同意使用json,那么这个怎么样

>>> print mytext

{"bigPictureUrl":"https://fbcdn-profile-a.akamaihd.net/*-*-*/*.*.*.*/*/*.jpg", 
"timelineCoverPhoto":"{"focus":{"x":0.5,"y":0.49137931034483},"photo":{"__type__

":{"name":"Photo"},"image_lowres":{"uri":"https://fbcdn-*-*-*.*.*/*-*-*/*.jpg
","width":180,"height":135}}}",
    "subscribeStatus":"IS_SUBSCRIBED","smallPictureUrl":"https://fbcdn-profile-a.akamaihd.net/*-*-*/*.*.*.*/*/*.jpg",

>>> uri = re.findall(r'uri\"\:\"[\'"]?([^\'" >]+)', mytext) #gets the uri
>>> smallpicurl = re.findall(r'smallPictureUrl\"\:\"[\'"]?([^\'" >]+)', mytext) # gets the smallPictureUrl
>>> ''.join(uri).rstrip()
'https://fbcdn-*-*-*.*.*/*-*-*/*.jpg' # uri
>>> ''.join(smallpicurl).rstrip()
'https://fbcdn-profile-a.akamaihd.net/*-*-*/*.*.*.*/*/*.jpg' # smallPictureUrl
#参见:http://daringfireball.net/2010/07/improved_regex_for_matching_urls
导入re、urllib
(以下:https::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::[[[目前目前目前鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于目前目前目前目前目前目前目前目前目前目前目前目前目前目前目前目前目前的的的的(若若若若若若若若若若若若c\u201d\u2018\u2019]))
对于urllib.urlopen(“http://daringfireball.net/misc/2010/07/url-matching-regex-test-data.text"):
为GRUBER\u URLINTEXT\u PAT.findall(行)中的MGROUP打印[MGROUP[0]
#参见:http://daringfireball.net/2010/07/improved_regex_for_matching_urls
导入re、urllib
(以下:https::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::[[[目前目前目前鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于鉴于目前目前目前目前目前目前目前目前目前目前目前目前目前目前目前目前目前的的的的(若若若若若若若若若若若若c\u201d\u2018\u2019]))
对于urllib.urlopen(“http://daringfireball.net/misc/2010/07/url-matching-regex-test-data.text"):
为GRUBER\u URLINTEXT\u PAT.findall(行)中的MGROUP打印[MGROUP[0]

行是字符串不是json,所有文件都是sting。对于使用real parser(json)而不是特殊正则表达式,它已使用linux命令字符串+1格式化。@user1592211:问题中的输入数据是行是字符串不是json,所有文件都是sting。对于使用real parser,它已使用linux命令字符串+1格式化(json)而不是特殊正则表达式。@user1592211:问题中的输入数据看起来很棒,但尽管它在脚本中运行时没有错误,但它没有打印任何内容,我确信我遗漏了一些内容。在空闲状态下,mytext是黑色的,但在脚本中,我写的行不是黑色的,它将其作为字符串(相同颜色)还有一些字符,它们与rest字符串的颜色不同,黑色字符与idleOn idle不同,使用三个单引号(“”)初始化mytext变量。正如mytext=''your value''在空闲状态下一样,我用单引号运行它没有问题,但我必须在一个文件上执行此操作。我将变量中的一行保存为字符串,以测试它是否有效,我没有输出,我打印uri并返回一个空列表。我发布的所有内容都是单行。你能粘贴这一行吗从代码中为变量赋值行..?我通过在for循环中为该行使用append,列出了所需的所有行,然后赋值变量=li[1]对于列表中的一个元素来说,要测试它是否工作,它是字符串,我正试图让它像上面所说的那样工作,它看起来很棒,但尽管它在脚本中运行,没有错误,但它没有打印任何内容,我确信我遗漏了一些内容。在空闲状态下,我的文本是黑色的,但在脚本中,我写的行不是黑色的,它将它作为字符串(相同的颜色)还有一些字符,它们与rest字符串的颜色不同,黑色字符与idleOn idle不同,使用三个单引号(“”)初始化mytext变量。正如mytext=''your value''在空闲状态下一样,我用单引号运行它没有问题,但我必须在一个文件上执行此操作。我将变量中的一行保存为字符串,以测试它是否有效,我没有输出,我打印uri并返回一个空列表。我发布的所有内容都是单行。你能粘贴这一行吗从代码中为变量赋值行..?我通过在for循环中为该行使用append,列出了所需的所有行,然后我为列表中的一个元素赋值variable=li[1],以测试它是否有效,该元素是string,我正试图使它像上面所述的那样工作