Python: Check 2 text files for string and print contexts


 
Thread Tools Search this Thread
Top Forums Programming Python: Check 2 text files for string and print contexts
# 1  
Old 01-30-2013
Python: Check 2 text files for string and print contexts

I have 2 text files:

cities.txt
Code:
San Francisco
Los Angeles
Seattle
Dallas

master.txt
Code:
Atlanta is chill and laid-back.
I love Los Angeles.
Coming to Dallas was the right choice.
New York is so busy!
San Francisco is fun.
Moving to Boston soon!
Go to Seattle in the summer.

output.txt
Code:
<main><beg>I love</beg><key>Los Angeles</key><end></end></main>
<main><beg>Coming to</beg><key>Dallas</key><end>was the right choice</end></main>
<main><beg></beg><key>San Francisco</key><end>is fun</end></main>
<main><beg>Go to</beg><key>Seattle</key><end>in the summer</end></main>

Each entity in cities.txt is the <key>. The master.txt file is much longer, and all lines without the city in cities.txt should just be ignored. They're not in order. The output prints out the cities in <key> and <beg> & <end> context (if any).

This is what I have:
Code:
with open(master.txt) as f:
    master = f.read()
working = []
with open(cities.txt) as f:
    for i in (word.strip() for word in f):
        if i in master:
                print "<key>", i, "</key>"

I can find the cities in the master file and print them out to a new file, but I'm stuck at how I can also print out the beginning and end contexts for the lines with the cities

Last edited by Scrutinizer; 01-30-2013 at 05:34 PM.. Reason: extra code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Print a python script down a list in a text file without printing a lot combinations

In a python script I have 2 files printing side by side on the same line. I want to have 1 of the files to be already displayed at once while the other file print down the list in the file and it still will produce new lines. I want to do it like that to reduce printing a lot of lines and... (0 Replies)
Discussion started by: bigvito19
0 Replies

2. Shell Programming and Scripting

How to check in a folder all files which doesnt have the specifc string?

I have a folder with 100s of dat files, with delimiter "|", in some files they didn't provide this delimiter. how to automatically check those list of files in a folder which doesnt have a delimiter or string this way "|" is it possible? Thank you very much for the helpful info. (3 Replies)
Discussion started by: cplusplus1
3 Replies

3. Shell Programming and Scripting

Script to check the string in a file and print an attribute in the message

Hi, I am new to shell scripting and got a task to complete. Task is : we have a log file where in i need to traverse through the whole file to check the string "Person Type missing in message" and after that i need to get EMPLID=xxxxxx from the file and print details in a different file. ... (1 Reply)
Discussion started by: suren424
1 Replies

4. Shell Programming and Scripting

Match string from two files and print line

Hi, I have been trying to find help with my issue and I'm thinking awk may be able to do it. I have two files eg file1.txt STRING1 230 400 0.36 STRING2 400 230 -0.13 STRING3 130 349 1 file2.txt CUFFFLINKS 1 1394 93932 . + STRING1 CUFFFLINKS ... (9 Replies)
Discussion started by: zward
9 Replies

5. UNIX for Dummies Questions & Answers

Print only '+' or '-' if string matches (two files)

I would like to add two additional conditions to the actual code I have: print '+' if in File2 field 5 is greater than 35 and also field 7 is grater than 90. while read -r line do grep -q "$line" File2.txt && echo "$line +" || echo "$line -" done < File1.txt ' Input file 1: ... (5 Replies)
Discussion started by: bernardo.bello
5 Replies

6. Shell Programming and Scripting

How do you check for a particular string in 1000's of files?

Please forgive my ignorance on scripting. I am trying to determine (via a script) if a certain string of characters is present . The string that I am looking for is a constant length. Here is the string I am searching for: Model_Type={t}, ModelName={m} I need to determine if the above... (2 Replies)
Discussion started by: dlundwall
2 Replies

7. Shell Programming and Scripting

Compare two text files and print matches

Hi, I am looking for a way to compare two text files and print the matches. For example; File1.txt 89473036 78474384 48948408 95754748 47849030 File2.txt 47849030 46730356 16734947 78474384 36340047 Output: (11 Replies)
Discussion started by: lewk
11 Replies

8. Shell Programming and Scripting

Find text containing paths and replace with a string in all the python files

I have 100+ python files in a single directory. I need to replace a specific path occurrence with a variable name. Following are the find and the replace strings: Findstring--"projects\\Debugger\\debugger_dp8051_01\\debugger_dp8051_01.cywrk" Replacestring--self.projpath I tried... (5 Replies)
Discussion started by: noorsam
5 Replies

9. Shell Programming and Scripting

print string at the end of lines in text file

hello, I go text file like this E:/DDD/Dyndede/wwww E:/DDD/sss.com/ffffg/fff E:/DDD/vvvvvv/dd E:/DDD/sss.com/bbbbbb E:/DDD/sss.com/nnnn/xxI want to print /alpha.jpg at the end of every lines like that E:/DDD/Dyndede/wwww/alpha.jpg E:/DDD/sss.com/ffffg/fff/alpha.jpg... (8 Replies)
Discussion started by: davidkhan
8 Replies

10. Shell Programming and Scripting

Check all files in directories for string and remove.. possible?

Say I am in /var/adm/bin and I want to search through all the various scripts and such in that directory for a string, say, xxx@yyy.com I want to find every file with that in there and replace it with a single space. Is that possible? Or, is it possible to search every file and get a list... (7 Replies)
Discussion started by: LordJezo
7 Replies
Login or Register to Ask a Question