I want to make a small command-line-run program that looks for a word or phrase in a target .txt. or .docx file, prints out a corresponding blurb about that phrase, and then replaces it with a different word/phrase. There are about 200 of these tuples.

What is the best way to structure this? I'm thinking of putting this info into a text file and writing code to read it in. Or should I simply write it all into the program?

I'm also thinking of making a class for each tuple w/ three fields that stores this information.

Is this a good approach, or are there better ways?

PS: I'm thinking of using Java for this unless someone can suggest Python for a particular reason.

  • 1
    The proposed solution seems fine to me. I would use a text file for ease of formatting and ease of updating with new tuples later on (if applicable).
    – Dan
    Jul 5 '16 at 14:39

The way I interpret your idea:

You have about 200 strings that you want to search for in your target .txt

  1. Find any of these given strings
  2. Give the user(?) som kind of feedback
  3. Replace it with some other string

Both strings returned in step 2 and 3 are mapped to the exact content of the string found in step 1, i.e:

'a' found -> print 'a_feedback', put 'a_replace'
'b' found -> print 'b_feedback', put 'b_replace'

Load these respective string-tuples from some kind of source .txt and store them , each in their own object of some custom tuple-class.


You've got a solution going! Java is probably a good way to go for this but I don't see why Python should be turned down either. Go with what you're most confident using.

I also suggest you go on with your plan to separate your text-source from the code. Since you're going to have a large mass of distinct text-tuples there's really no sense in interspersing it into your source code, it will just be messy.

You could come up with some kind of simple custom structure for storing your data in the .txt and then use regex to find and parse every tuple, however, I would suggest that you use some more conventional way of storing data. My suggestions:

  • For simplicity: INI
  • For something simple but really useful: JSON
  • For broad usability and expandability: XML

For storing your tuples when loaded into the main program, objects is the way to go. I think a custom class for this kind of tuple sounds like a good plan, but you could also play around with maps for mapping the key-string to it's feedback and replace values, respectively.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.