I am trying to get the digits from the second last column in this txt file url: http://services.swpc.noaa.gov/text/wing-kp.txt
I only need the last value in the second last column at the very end of the file.
I have tried a few different sample-codes in Python 3(.4?)
This code only get me a specific amount of characters starting from the beginning of the file:
# coding: utf-8
import urllib.request
req = urllib.request.Request('http://services.swpc.noaa.gov/text/wing-kp.txt')
with urllib.request.urlopen(req) as response:
the_page = response.read(100)
print (the_page)
I have tried the .seek function but it returned a value I could not recognize.
In the following code I first tried to use the .seek directly from the webpage, but it didn't work so then I tried to save the file first and then read from the file with no/limited success.
# coding: utf-8
import urllib.request
req = urllib.request.Request('http://services.swpc.noaa.gov/text/wing-kp.txt')
with urllib.request.urlopen(req) as response:
open('data.txt', 'wb').write(urllib.request.urlopen(req).read())
file = open('data.txt' , 'rb+')
data = file.seek(-5, 2)
file.close()
print (data)
If you only need the second last value, you could do it like this:
file = open('data.txt' , 'rb+')
data = file.readlines()
file.close()
data = [i for i in str(data[-1]).strip().split(" ") if i != ''][-2]
With file.readlines()
we get a list of all the lines, where we can take the last by indexing with [-1]
. Then, we can simply split by whitespaces and construct a new list with all non-empty strings, where we now have the second last column as the second last element of the list. This assumes that there are no whitespaces in the values for the last two columns and does not work for parsing all columns, since other data like the dates is also separated by whitespaces.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments