How to retrieve the last characters from a remote text file using Python?

Tore André Rosander

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)
Tristan

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.

edited at
0

Comments

0 comments
Login to comment

Related

How to retrieve partial text from a text node using Selenium and Python

Is it possible to retrieve one string between 2 special characters from text file using bash?

How to find a substring in a line from a text file and add that line or the characters after the searched string into a list using Python?

C# How to delete last 2 characters from text file and write into the same line at the end my text?

How to retrieve plain text from .doc files using Python?

How to retrieve only last 4 characters in a string using XPath Query

Python: How to replace the last word of a line from a text file?

how to select the last characters of a file name in python

how to retrieve data from json file using python

How to retrieve one column from csv file using python?

retrieve lines from a text file in python and

How to read text from a file using Python?

Reading a remote text file using python

How to remove the last occurrence of "=" symbol from a property file using Python

counter characters from a text file in python

Splitting characters from a text file in Python

Retrieve records from a log file for last n seconds in python

How to delete last 6 characters from a column in .xlsx file using R?

How to retrieve the text of a WebElement using Selenium - Python

Python - How to retrieve certain text from a website

How to print special characters from a text file

How to remove special characters from text file

How to extract characters from text file

Print last N characters from all lines in a file using cut

Java: retrieve and replace the last line in a text file

How to remove the last characters in a file name using Bash?

retrieve phone number from text file using indexOf

Retrieve text from a large file using a smaller matching header

remove the last two characters from strings in list using python

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive