Python - Finding and multiplying an integer after specific word

Matt

I have a text file that has over 60,000 lines in it and I need to go through and find a specific word and then multiply the number after it. For example:

The cat jumped over the log
  tree 6
the dog also jumped over the log
tree 43

Would end up, if multiplying by 2, as:

The cat jumped over the log
  tree 12
the dog also jumped over the log
tree 86

I know this is fairly simple to do but I can't wrap my head around it. I need to step through the whole text file word by word looking for instances of "tree ", finding the integer after this, making the change, and then replacing the line.

I also know that every occurrence of "tree " will always have an integer next to it.

Thanks for your help.

Edit:

Current code I have as of now is:

file = open('export_desc.txt', 'r')

a = "ext_mpl "
for line in file:
    n = file.find(a+1)
    n = n*2
    file.write(line.replace(a+1, a+n))

file.close()
WKPlus

You don't even have to write a python script, a sed command can help you:

sed -r '/^tree ([0-9]+)$/{h;s/^.* ([0-9]+)$/echo \1*2|bc/e;H;g;s/[0-9]+\n//}' export_desc.txt

Sample:

>sed -r '/^tree ([0-9]+)$/{h;s/^.* ([0-9]+)$/echo \1*2|bc/e;H;g;s/[0-9]+\n//}' 123
The cat jumped over the log
tree 12
the dog also jumped over the log
tree 86
>cat 123
The cat jumped over the log
tree 6
the dog also jumped over the log
tree 43

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Python: Finding nearest integer value to specific integer

From Dev

Finding an equation that results in a specific integer, Python

From Dev

Finding specific positive integer

From Dev

Finding a specific nibble in an integer

From Dev

find specific word and read after that word in python

From Dev

Math error when multiplying float by integer in python

From Dev

Finding the Head Word in Python

From Dev

Finding file and replacing word with some specific word

From Dev

finding longest word in a list python

From Dev

Finding Word Stems in nltk python

From Dev

Finding a integer number after a beginning t=

From Dev

Finding a word in just a sentence and not in a word (python)

From Dev

Regex - Python: Capture three (3) words after a specific word

From Dev

extract few words before and after the specific word regex in Python?

From Dev

Finding a text in word and insert data after it

From Dev

finding word after the match using NSString

From Dev

Finding a text in word and insert data after it

From Dev

How to parse number after finding some word

From Dev

Python crawler not finding specific Xpath

From Dev

Python Code for Finding Specific Parameters

From Dev

Javascript : Get word after specific word?

From Dev

Javascript : Get word after specific word?

From Dev

Extract first word after specific word

From Dev

How to retrieve word after specific word in Java?

From Dev

Select substring after specific word

From Dev

Extracting string after specific word

From Dev

get value after specific word

From Dev

Python: Finding Lowest Unique Integer from List

From Dev

Regular expression to extract all the word after a specific symbol in a word using python

Related Related

HotTag

Archive