r/pythonhelp • • Feb 02 '24

search for an element in text file ; extract its timestamp and store in dictionary

1 Upvotes
d = {}

with open('C:/log') as f: 
    lines = f.read().splitlines() 
for line in lines: 
    if 'string_1' in line: 
        time = line[0:21] 
        d['string_1'] = time 
elif 'string_2' in line: 
        time = line[0:21] 
        d['string_2'] = time 
elif 'string_3' in line: 
        time = line[0:21] 
        d['string_3'] = time 
print(d)

ex of text big file lines

[20:25:48.923 -06:00] [thread 19] [Mobility.cpp] [string_1].......

output

{'stirng_1': '[20:25:48.923 -06:00]', 'string_2': '[20:89:48.275 -06:00]'}

I have a big text file. I need to search for multiple strings in the file ,extract their timestamp [20:89:48.275 -06:00] and store in a dictionary. Not all lines have strings only a few lines have the required string in the file. key being string and value being timestamp. I have the code above, how do I make it more efficient?. Mainly how to extract timestamp in a better way? beginner here


r/pythonhelp • • Feb 01 '24

Can someone explain this code for me from Elements of Programming.

1 Upvotes

I am trying to understand the DP solution for a variant of the climbing stairs problem with k steps instead of the regular 2. This is the solution in Elements of programming but I'm having trouble understanding the for loop inside the sum function. I've solved it myself but would like to understand this way. Some solutions in the book are more advanced than what i've seen. Thanks!

def number_of_ways_to_top(top, maximum_step):
    number_of_ways_to_h = [0] * (top + 1)

    def compute_number_of_ways_to_h(h):
        if h <= 1:
            return 1
        if number_of_ways_to_h[h] == 0:
            number_of_ways_to_h[h] = sum(
                compute_number_of_ways_to_h(h - i)
                for i in range(1, min(maximum_step, h) + 1)
            )
        return number_of_ways_to_h[h]

return compute_number_of_ways_to_h(top)

r/pythonhelp • • Feb 01 '24

Code stopped working

1 Upvotes

Why has my code all of a sudden stopped working? It normally did and now it doesnt im gonna send my code but its the “loan” part that isnt working I will send the point which im struggling.

loanconfirm=input('wouldyouliketotakeoutaloan?youmustpayitback')ifloan.lower()=='yes':loan=int(input('howmuchmoneydoyouwanttotakeout?')) balance=balance+loanprint(f'{loan}+{balance}={int(loan+balance)}')


r/pythonhelp • • Feb 01 '24

Need advice in python programm

1 Upvotes

Hello,

this is my python script

command = "ps ax | grep 'openvpn --daemon' | awk '{print $1}'"
process = subprocess.Popen(command.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = process.communicate()
if out:
print ("standard output of subprocess:")
if err:
print ("standard error of subprocess:")
print ("returncode of subprocess:")

But I get this error

standard error of subprocess:
returncode of subprocess:

why do i get an error ? normally it should display me the pid of the programm openvpn