1

I am a newbie to python.Consider I have a list ['python','java','ruby']

I have a textfile as:

jrubyk
knwdjavawe
weqkpythonqwe
1ruby.e

Expected output:

ruby
java
python
ruby

I need to print the strings in list hidden inside as substring. Is there a way to obtain that?

3
  • No if you dont have a database or a reference as you are giving with the words you are expecting to output. If you do, it doesnt make sense. You dont need the file to do so. Just iterate over the items and print them Commented Sep 5, 2014 at 16:21
  • @ShashankAgarwal maybe he wasnt able to do anything . Anyway, what he is trying to do is no way useful but to practice the programming language Commented Sep 5, 2014 at 16:23
  • I've changed the sample input in the question to make the point clear. Commented Sep 6, 2014 at 15:32

2 Answers 2

2

I tend to use regular expressions when I want to strip certain substrings from larger strings. Here is an inelegant but readable way to do this.

import re

python_matcher = re.compile('python')
java_matcher = re.compile('java')
ruby_matcher = re.compile('ruby')

hidden_text_list = open('hidden.txt', 'r').readlines()

for line in hidden_text_list:
    python_matched = python_matcher.search(line)
    java_matched = java_matcher.search(line)
    ruby_matched = ruby_matcher.search(line)

    if python_matched:
        print python_matched.group()
    elif java_matched:
        print java_matched.group()
    elif ruby_matched:
        print ruby_matched.group()
Sign up to request clarification or add additional context in comments.

3 Comments

Ooh this is fun! Very good for a long list, not so great for a short list of hidden words. Try all_matcher = re.compile("|".join([re.escape(word) for word in hidden_words])) for line in textfile: print(" ".join(all_matcher.findall(line)))
I'll leave the answer here, but in hindsight I like yours much better. More concise. I started to get froggy on the regex objects, but wanted OP to be able to understand what was going on. I really like your findall method, though.
Feel free to edit mine in as an alternative. I didn't even consider regex here, oops!
1

The brute force approach is:

hidden_strings = ['python','java','ruby']
with open('path/to/textfile/as/in/example.txt') as infile:
    for line in infile:
        for hidden_string in hidden_strings:
            if hidden_string in line:
                print(hidden_string)

3 Comments

@wonderwhy The question makes perfect sense. He has a list of hidden words and a textfile with gobble-dee-gook that contains hidden words in it. He's trying pull his hidden words out of the cough "ENCRYPTED" text file.
But why to search the words inside the file if you already have them, @AdamSmith?
@wonderwhy "Why" isn't really a valid question here, but it seems like he's pulling a message from the textfile, not the list....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.