codingdir logo sitemap sitemap |
Home
PHP
C#
C++
ANDROID
JAVA
JAVASCRIPT
PYTHON

Finding numbers after a certain keyword using Python


By : , Category : python

You can use findall() with a positive look behind:

>>> import re
>>>
>>> s = ">>1244"
>>> re.findall(r"(?<=>>)d+", s)
['1244']
>>>
>>> s = ">>1244 something >>500"
>>> re.findall(r"(?<=>>)d+", s)
['1244', '500']

Here the (?<=>>)d+ expression would match one or more digits (d+) that go after the >>.

ReLated :

You can change your printing routine to the following

# Convert to a string
str_num = str(num)
# Check whether the length of the string is equal to the number
# of unique digits using a set
repeating = len(set(str_num)) != len(str_num)
# Print if there are no repeating digits
if not repeating:
    print num

The following function will open a file, scan through it until it finds the search term, then get all the lines beginning with the data4 line until a blank line is reached.

def get_data4(filename, search_term):
    out = []  # will accumulate data4 lines

    with open(filename) as f:
        # find search term
        for line in f:
            if line.strip() == search_term:
                break

        # skip first three data items
        next(f)
        next(f)
        next(f)

        # find data4 (all lines until blank line)
        for line in f:
            if not line.strip():
                break

            out.append(line)

    return ''.join(out)  # concatenate data4 back into a string

Use it like this:

data4 = get_data4('my_data.txt', 'X_string2')
print(data4)

Something like this?

sFILE = 'Main keyword:Mainvariable, variable (1):name_1, variable (2, 3):name_2, variable(3, 1, 2):name_3'

# I'm removing the beginning, but you could extract it if you want to check it
# ie "Main keyword:Mainvariable,"
sLINE = sFILE[26:]
# split the rest of the line at "variable"
sVARS = sLINE.split('variable')

for item in sVARS:
  # clean up whitespaces and commas
  item = item.strip(' ,')
  print('"',item,'"')

# prints "(1):name_1"
# prints "(2, 3):name_2"
# prints "(3, 1, 2):name_3"

Now you can extract the name_n or do some other necessary operation or do the same thing in your other file and cross check? eg populate the names in a list (or dic depending on your need) and then in the other file extract the "1" "2" and "3" and look up the list/dictionary?

This task is the kind of thing Python's generators were created for.

First we'll make a generator that grabs each line with contents you are interested in:

def get_interesting_line(file,*searches):
    with open(file,'r') as f:
        for line in f:
            if all(search in line for search in searches):
                yield line

# Use it like this:
interesting_lines = get_interesting_line(myfile,':command','started.')
next(interesting_lines)
# Or like this:
for line in get_interesting_line(myfile,':command','started.'):
    print(line)

Then we'll make another generator that returns a tuple made up of an item and the following item (or in other words, a previous item and current item) from any other generator/iterator:

def get_item_pair(iterable):
    previous = next(iterable)
    for current in iterable:
        yield previous, current
        previous = current

# Use it like this:
line_pairs = get_item_pair(get_interesting_line(myfile,':command','started.'))
next(line_pairs)
# Or like this:
for pair in get_item_pair(get_interesting_line(myfile,':command','started.')):
    print(pair)

Now we can put it all together and create another generator which will spit out a four-tuple composed of data, value, previous time and current time based on your criteria (we will use the datetime module to store the times as datetime objects).

from datetime import datetime
def get_command_file_delta_t_info(file):
    c_lines = get_interesting_line(file,':command','started.')
    line_pairs = get_item_pair(c_lines)
    for line1, line2 in line_pairs:
        value1 = line1.split('command: ')[1].split(' started.')[0]
        value2 = line2.split('command: ')[1].split(' started.')[0]
        if value1 != value2:
            #value changed
            year1 = line1.split(' ',1)[0]
            year2 = line2.split(' ',1)[0]
            #assuming date is MM/DD format:
            month1 = line1.split()[1].split('/')[0]
            month2 = line2.split()[1].split('/')[0]
            day1 = line1.split()[1].split('/')[1]
            day2 = line2.split()[1].split('/')[1]
            #now hours, minutes, seconds:
            hour1 = line1.split()[2].split(':')[0]
            hour2 = line2.split()[2].split(':')[0]
            minute1 = line1.split()[2].split(':')[1]
            minute2 = line2.split()[2].split(':')[1]
            second1 = line1.split()[2].split(':')[2]
            second2 = line2.split()[2].split(':')[2]
            #now create a couple of date time objects for initial and final times
            dt_i = datetime(year1, month1, day1, hour1, minute1, second1)
            dt_f = datetime(year2, month2, day2, hour2, minute2, second2)
            #strip out data
            data = line2.split(':command ')[1].split(" " value2)[0]
            #finally yield a tuple containing the data you want
            yield (data, value2, dt_i, dt_f)

As explained above, this generator is yielding items in the following format:

#create the generator
my_gen = get_command_file_delta_t_info(myfile)
#Get first generated item:
next(my_gen)
#produces:
(data1, value1, dt_i1, dt_f1)
#Now we can get the second item, 3rd, etc:
next(my_gen)
(data2, value2, dt_i2, dt_f2)
#get all the remaining items and do stuff with them:
for item in my_gen:
    do_stuff(item)
    # note that the first item in the for statement is actually the 3rd
    # item that has been generated. the first two were generated in the 
    # lines above. my_gen does not "start over".

As illustrated above, the generator is an iterable object that only proceeds to the next item in the generator when the generator is iterated over, e.g., using next(), or in a for statement.

Now we can do whatever we want with the file data, including make a function that will output it in the format we want:

def print_command_file_delta_t_info(command_file_delta_t_info):
    for info in command_file_delta_t_info:
        #get the time difference
        #time_diff is a datetime.timedelta object
        time_diff = info[3] - info [2]
        #now print the information you want:
        print("{data} {value} : {minutes} minutes".format(data = info[0], value = info[1], minutes = time_diff.total_seconds()/60))
Comments


Message :
Login to Add Your Comments .
How to disable registered OpenCL platforms on Windows?
Is Observable broken in Angular 2 Beta 3?
Cross-thread operation not valid when using Invoke
How to pass an IEnumerable or queryable list of properties from Controller to View
Finding numbers after a certain keyword using Python
Pocketsphinx recognizes random phrases in a silence
Passing non-thread-safe objects through thread-safe containers
React scroll nav
BizTalk WCF-BasicHttp Adapter does not allow Empty string for Service Certificate Props
Why property ''cause" of Exception is repeating forever?
Privacy Policy 2017 © codingdir.com All Rights Reserved .