Home > OS >  search the dictionary for more data
search the dictionary for more data

Time:06-05

I created this script I tried various things here and nothing helped so I would like to contact you as more experienced people

basically the script principle what does it do?

the code I put here does this

input

1:1:1

output

1:1:1 this is text from file

it works great but if i want to search for two results so it doesn't work here i try it in several ways

the problem is that I want to look for two things at the same time

input 1:1:1 === 4:1:2===0---0 I'll do a split here

split ("===") [0]
split ("===") [1]
split ("===") [2]

and the output I'm looking for is

1: 1: 1 this is text from file 4: 1: 2 this is next text resolut 0 --- 0

from the file from which it takes data looks something like this

"numberssearch.txt"

1:1:1===4:1:2===0---0
1:1:1===4:2:3===-1----1
1:1:1===4:3:4===-2----2
1:1:1===4:4:5===-3----3
1:1:1===4:5:6===-4----4
1:1:1===4:6:7===-5----5

and this is what the file from which it takes data looks like

"book.txt"

1:1:1   text1
1:1:2   text2
1:1:3   text3
1:1:4   text4
1:1:5   text5
1:1:6   text6
1:1:7   text7
ect... 

this is code

with open ("numberssearch.txt") as f:
         unsolved = set (f.read (). split ())
for unsolved_elem in unsolved:
    userinput = unsolved_elem
    file =  open("book.txt",encoding='ISO-8859-1')
    lines = file.readlines()
    for line in lines:
        words = line.split()
        unsolved_elem = unsolved_elem.split("===")[0]
        if len(words) > 0 and unsolved_elem == words[0]:
           print(line, file=open("" str("solved") ".txt", "a",encoding='ISO-8859-1'))

CodePudding user response:

it would need to run two nested loops - and it would need to read from file book in two loops - so it would be better first to read data book.txt and create dictionary.


I use io only to simulate file in memory - so everyone can simply copy and run it.
But you should use open()

In this version I assumed that book may have the same number many times so it needs list to keep all text for this number.

book_txt = '''1:1:1   text1
1:1:2   text2
1:1:3   text3
1:1:4   text4
1:1:5   text5
1:1:6   text6
1:1:7   text7
4:2:3   other3
4:2:4   other4
4:4:5   other5'''

import io

# --- read book.txt ---

book = {}

#with open("book.txt") as f:
with io.StringIO(book_txt) as f:
    for line in f:
        number, text = line.strip().split(" ", 1)
        if number not in book:
            book[number] = []
        book[number].append(text)
        
for item in book.items():
    print(item)

Result:

('1:1:1', ['text1'])
('1:1:2', ['text2'])
('1:1:3', ['text3'])
('1:1:4', ['text4'])
('1:1:5', ['text5'])
('1:1:6', ['text6'])
('1:1:7', ['text7'])
('4:2:3', ['other3'])
('4:2:4', ['other4'])
('4:4:5', ['other5'])

And similar with file numberssearch.txt - first I would read all line and run split('===')

numberssearch_txt = '''1:1:1===4:1:2===0---0
1:1:1===4:2:3===-1----1
1:1:1===4:3:4===-2----2
1:1:1===4:4:5===-3----3
1:1:1===4:5:6===-4----4
1:1:1===4:6:7===-5----5'''

import io

# --- read numberssearch.txt ---

unsolved = set()

#with open ("numberssearch.txt") as f:
with io.StringIO(numberssearch_txt) as f:
    for line in f:
        parts = tuple(line.strip().split('==='))
        unsolved.add(parts)

for item in unsolved:
    print(item)

Result:

('1:1:1', '4:5:6', '-4----4')
('1:1:1', '4:4:5', '-3----3')
('1:1:1', '4:1:2', '0---0')
('1:1:1', '4:3:4', '-2----2')
('1:1:1', '4:6:7', '-5----5')
('1:1:1', '4:2:3', '-1----1')

And later I would search numbers from book in unsolved

for number1, number2, rest in unsolved:

    if (number1 in book) and (number2 in book):

        # nested loops which use `book`
        for text1 in book[number1]:
            for text2 in book[number2]:
                print(number1, text1, number2, text2, rest)

Result:

1:1:1 text1 4:4:5 other5 -3----3
1:1:1 text1 4:2:3 other3 -1----1

Full working code:

book_txt = '''1:1:1   text1
1:1:2   text2
1:1:3   text3
1:1:4   text4
1:1:5   text5
1:1:6   text6
1:1:7   text7
4:2:3   other3
4:2:4   other4
4:4:5   other5'''

numberssearch_txt = '''1:1:1===4:1:2===0---0
1:1:1===4:2:3===-1----1
1:1:1===4:3:4===-2----2
1:1:1===4:4:5===-3----3
1:1:1===4:5:6===-4----4
1:1:1===4:6:7===-5----5'''

import io

# --- read book.txt ---

book = {}

#with open("book.txt") as f:
with io.StringIO(book_txt) as f:
    for line in f:
        number, text = line.strip().split(" ", 1)
        if number not in book:
            book[number] = []
        book[number].append(text)
        
for item in book.items():
    print(item)

# --- read numberssearch.txt ---

unsolved = set()

#with open ("numberssearch.txt") as f:
with io.StringIO(numberssearch_txt) as f:
    for line in f:
        parts = tuple(line.strip().split('==='))
        unsolved.add(parts)

for item in unsolved:
    print(item)
    
# --- search ---

for number1, number2, rest in unsolved:

    if (number1 in book) and (number2 in book):

        for text1 in book[number1]:
            for text2 in book[number2]:
                print(number1, text1, number2, text2, rest)

If book.txt may have every number only once then code can be simpler

book_txt = '''1:1:1   text1
1:1:2   text2
1:1:3   text3
1:1:4   text4
1:1:5   text5
1:1:6   text6
1:1:7   text7
4:2:3   other3
4:2:4   other4
4:4:5   other5'''

numberssearch_txt = '''1:1:1===4:1:2===0---0
1:1:1===4:2:3===-1----1
1:1:1===4:3:4===-2----2
1:1:1===4:4:5===-3----3
1:1:1===4:5:6===-4----4
1:1:1===4:6:7===-5----5'''

import io

# --- read book.txt ---

book = {}

#with open("book.txt") as f:
with io.StringIO(book_txt) as f:
    for line in f:
        number, text = line.split()
        book[number] = text
        
for item in book.items():
    print(item)

# --- read numberssearch.txt ---

unsolved = set()

#with open ("numberssearch.txt") as f:
with io.StringIO(numberssearch_txt) as f:
    for line in f:
        parts = tuple(line.strip().split('==='))
        unsolved.add(parts)

for item in unsolved:
    print(item)
    
# --- search ---

for number1, number2, rest in unsolved:
    if (number1 in book) and (number2 in book):
        text1 = book[number1]
        text2 = book[number2]
        print(number1, text1, number2, text2, rest)
  • Related