1

I am trying to teach myself Python to see whether I can use it in my research to handle some largish data files. I have been looking around and reading questions that have been asked here, but have not managed to make the file look up work. I have the following: In data.txt I have one word per line. And dict.txt is a JSON file where I have among others "word": "house", "meaning": "Haus".

I would like to look up each word of data.txt in dict.txt, and then add "meaning" next to the word of data.txt. So, if I have:

This
is
my
house

Processing it would result in:

This Dies
is ist
my mein
house Haus

Here, I found this piece of code, but nothing happens if I run it:

with open('data.txt') as f:
    haystacks = list(f)

with open('dict.json') as f:
    for line in f:
        needle = line.strip()
        for haystack in haystacks:
            if needle in haystack:
                print(haystack)

Is this even the right direction, or should I look at other modules? I am lost and would appreciate some pointers in the right direction.

martineau
  • 119,623
  • 25
  • 170
  • 301
Rava
  • 11
  • 1
  • You'll learn a lot by getting a Python source debugger, stepping through your code and viewing the variables as they change. – Mark Tolonen May 11 '21 at 00:56
  • I would suggest reading the JSON file with Python's [`json`](https://docs.python.org/3/library/json.html#module-json) module, which will turn its contents into a Python data structure (usually a dictionary) for you to use. Afterwards, all you need to do is read each line of the data file, extract the word from it, and look that up in the dictionary to get its corresponding value. Then all you have to do is print the two on the same line. – martineau May 11 '21 at 02:21
  • Thank you very much @MarkTolonen and martineu. I have looked into IDLE, but it was not very straightforward to see what happens. Will look into it. – Rava May 12 '21 at 09:28

1 Answers1

0

Welcome to python!

What you are asking is fairly straightforward. A quick and dirty example is below with some name changes that make things a bit more clear (hopefully). The only thing your example is missing is the "processing" component you are referring to. In the case of my example, all I am doing is printing the key and the value together rather than apart. This is where you can do whatever you'd like.

import json

# Open the json file.
with open ("./dict.json") as f:
    # Load the JSON and put it in a dictionary object
    dictionary = json.load(f)

# Open the data file.
with open ("./data.txt") as d:
    # For each line in the file
    for line in d:
        # Remove the newline character
        stripped_line = line.rstrip()
        # If the stripped line is in the dictionary
        if stripped_line in dictionary:
            # Print it out (or do whatever else you'd like)
            print(f'{stripped_line}:{dictionary[stripped_line]}')
MKC
  • 85
  • 4
  • Thank you very much for this code, @mkc, and your comments. I seem to understand your code, but am unable to run it successfully. That is, when I run it, nothing happens. I redirect the output to another file on Linux, which remains empty. I don't think it is an issue of a bug, but perhaps rather my inability to run it properly!? – Rava May 12 '21 at 09:36
  • A couple of thoughts: The `./` component of `./dict.json` tells python to look in your current directory. If you don't have those files in your running directory, you aren't going to load any data to print. Are your data and dictionary files populated? If all this is a yes, how are you attempting to run it, through a command line? An IDE? I might be able to help with more info. – MKC May 13 '21 at 13:38