0
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

that's what i got from website, and i need to get only third value in every line.

i used this

filehandle = urllib.request.urlopen(url)

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

pass

how can i get specil value from every line?

inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241

4 Answers4

1

Split the line by \n (new-line character). For every non-empty line, split the line by space and take the third element (remember indexing starts at 0 in python):

>>> for line in mystr.split('\n'):
...     if not line:
...         continue
...     print line.split()[2]
... 
31.4
29.6
30.8
42.3
34.2
24.6

Or, the same in one line using list comprehension:

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

And, also, the same but with conversion numbers to float:

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

Hope that helps.

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
0
mybytes = filehandle.read()
mystr = mybytes.decode("utf8")
special_values = [line.split(' ')[2] for line in mystr.split('\n')]
JoseP
  • 611
  • 3
  • 6
0
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)

(with conversion of the values to numbers)

Aegis
  • 1,749
  • 11
  • 12
0

If your data is guaranteed to have 12 whitespace-separated columns on each line, then:

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

That is, take every twelfth element, starting at index n.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336