1

I'm trying to retrieve variables from a text file and pass those variables into python. Also in one text file i will have an content such as :

Total Numbers: 22, Total Boxes: 33, Total Tickets: 62, Total Files: 107
Total Numbers: 2, Total Boxes: 53, Total Tickets: 92, Total Files: 105
Total Numbers: 3, Total Boxes: 13, Total Tickets: 22, Total Files: 137
Total Numbers: 42, Total Boxes: 38, Total Tickets: 62, Total Files: 143

How can I only retrieve variables from the last line, for example i want to be able to pass Total Numbers: 42, then separately pass Total Boxes: 38, etc etc.

For example lets say i have a function in python

def get_content():
f = open('test.txt')
#not sure how to only retrieve last line
my_number = # this is where i want to pass Total_numbers to get a value of 42
f.close()
jumpman8947
  • 427
  • 2
  • 7
  • 17
  • 2
    You said you are trying to do that; care to edit your question and add the code you are trying? – Dimitris Fasarakis Hilliard Jan 12 '16 at 04:16
  • Possible duplicate of [Split string into a list in Python](http://stackoverflow.com/questions/743806/split-string-into-a-list-in-python) – Arc676 Jan 12 '16 at 04:17
  • Not sure whether you want to know how to use regex or just want to know how to only keep the last line – YCFlame Jan 12 '16 at 04:19
  • I just want to keep the last line. Not sure where to start. I want to be able to pass total numbers into a variable that is in python. – jumpman8947 Jan 12 '16 at 04:21
  • @jumpman8947 you may define a variable to keep the current line when you iterate the file, and you will get the last line after you hit the end of the file – YCFlame Jan 12 '16 at 04:30

3 Answers3

2

If the file isn't too big, just use readlines to read the lines into a list, making the last line easy to find. Once you have that line, split it on the comma and then again on spaces to isolate the values. I'm not sure what you mean by "passing" them... so here is an example that just extracts the 4 values, converts them to integers and returns them as a tuple.

def get_content():
    """"Returns (numbers, boxes, tickets, files) for the last entry
    in test.txt"""
    with open('test.txt') as fp:
        lines = fp.readlines()
    # just in case there is an empty line at the end...
    while lines and not lines[-1].startswith('Total Numbers'):
        del lines[-1]
    if lines:
        # split on comma then take the number at the end and return
        return tuple(int(part.split()[-1]) for part in lines[-1].strip().split(','))

print(get_content())
tdelaney
  • 73,364
  • 6
  • 83
  • 116
2

How can I only retrieve variables from the last line

In order to retrieve the last line in your data file, you might try

import subprocess
last_line = str(subprocess.check_output(['tail', '-1', 'data.txt']))

From there, splitting, e.g. as outlined by tdelaney, will give the needed values.

Klaus-Dieter Warzecha
  • 2,265
  • 2
  • 27
  • 33
1

If you just want to read the last line, and then get the number 42 out:

>>> f = open('somefile.txt')
>>> f.seek(-2, 2)
>>> while f.read(1) != b"\n":
...    f.seek(-2, 1)
...
>>> last_line = f.readline()
>>> last_line.split(',', 1)[0].split(':')[1].strip()
'42'

Now, if you want to get all the data out in a way that you can manipulate it later, you need to do some more work:

>>> import re
>>> e = r'Total (\w+): (\d+)'
>>> data = dict(re.findall(e, last_line))
>>> data['Tickets']
'62'
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284