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

Python:如何从行中找到特殊值?

Python:如何从行中找到特殊值?,python,url,Python,Url,这就是我从网站上得到的,我只需要在每行中获得第三个值 我用过这个 8.4 19.0 31.4 48.7 61.6 68.1 72.2 70.6 62.5 52.7 36.7 23.8 11.2 20.0 29.6 47.7 55.8 73.2 68.0 67.1 64.9 57.1 37.6 27.7 13.4 17.2 30.8 43.7

这就是我从网站上得到的,我只需要在每行中获得第三个值

我用过这个

8.4     19.0    31.4    48.7    61.6    68.1    72.2    70.6    62.5    52.7    36.7    23.8
11.2    20.0    29.6    47.7    55.8    73.2    68.0    67.1    64.9    57.1    37.6    27.7
13.4    17.2    30.8    43.7    62.3    66.4    70.2    71.6    62.1    46.0    32.7    17.3
22.5    25.7    42.3    45.2    55.5    68.9    72.3    72.3    62.5    55.6    38.0    20.4
17.6    20.5    34.2    49.2    54.8    63.8    74.0    67.1    57.7    50.8    36.8    25.5
20.4    19.6    24.6    41.3    61.8    68.5    72.0    71.1    57.3    52.5    40.6    26.2
如何从每行中获取特定值?

通过
\n
(新行字符)创建行。对于每一个非空行,按空格分割该行并获取第三个元素(记住,在python中索引从0开始):

filehandle = urllib.request.urlopen(url)

mybytes = filehandle.read()
mystr = mybytes.decode("utf8")
filehandle.close()
print (mystr)

pass
或者,在同一行中使用以下内容:

以及,同样的,但转换编号为:

希望有帮助

>>> [float(line.split()[2]) for line in mystr.split('\n') if line]
[31.4, 29.6, 30.8, 42.3, 34.2, 24.6]

(将值转换为数字)

如果保证数据每行有12个空格分隔列,则:

filehandle = urllib.request.urlopen(url)

mybytes = filehandle.read()
mystr = mybytes.decode("utf8")

special_values = [float(line.split(' ')[2]) for line in str.split('\n\n')]

filehandle.close()
print (mystr)
也就是说,每十二个元素取一次,从索引
n
开始

>>> [line.split()[2] for line in mystr.split('\n') if line]
['31.4', '29.6', '30.8', '42.3', '34.2', '24.6']
>>> [float(line.split()[2]) for line in mystr.split('\n') if line]
[31.4, 29.6, 30.8, 42.3, 34.2, 24.6]
filehandle = urllib.request.urlopen(url)

mybytes = filehandle.read()
mystr = mybytes.decode("utf8")

special_values = [float(line.split(' ')[2]) for line in str.split('\n\n')]

filehandle.close()
print (mystr)
mystr.split()[0::12] # first column
mystr.split()[1::12] # second column
mystr.split()[2::12] # third column
...                  # etc