Suppose I have this textfile A:

League 1:
a = 123456, b = england
a = 234564, b = brazil
a = 939311, b = germany
...
League 2:
a= 213213, b = italy
...
...
League 500:
a = 74778, b = denmark
...

-----------------------------------------------------

Now, I have another textfile B:

League 1:
trash
trash
trash
a = 123456
a = 234567
a = 939311
...
League 2:
trash
trash
...
a = 312311
a = 987324
...
...
League 500:
trash
trash
...
a = 238483
a = 243234
...

NOW, can someone please help me to convert a list of "a =" in each League from textfile B into a list of corresponding "b ="? Any ideas/ suggestions? Thanks in advance!

Created dictionary of values a as key.

Thanks Tony. I have tried to use a dictionary but my code, which I include below, does not run at all. I have waited for a day but nothing appears, not even an error message. Weird! Please help me, anyone!!

f = open('textfileA')

a = {}
l = 0

for line in f:
    line = line.strip()
    if "League" in line:
        a[l] = {}
        l += 1

    text = open('textfileB')
    for line in text:
        if line.lower().startswith(('NEXT LEAGUE', 'a = ')):

        ################
        #I forgot to include 'NEXT LEAGUE' right before the list of "a =" in textfileB
        ################

            my_a = line.split(',')[0]
            if my_a not in a[l-1]:
                a[l-1][my_a] = 1
            else:
                a[l-1][my_a] += 1

for i in a.keys():
    for val in a[i].keys():
        print val

You are opening an rereading B file for each line in A file, which does not make sense.

You could first work with file A alone and create a dictionary having the structure

{
    1: {
        '123456':'england',
        '234564': 'brazil',
        '939311': 'germany',
    },
    2: {
        '213213':'italy',
    },

    500: {
        '74778': 'denmark',
    },
}

then work with file B and this dictionary.

I have looked all over the net and tried but I'm still not entirely sure how to use a dictionary compiled from 1 file to convert between variables in another file. Can you please give me an example of how to do it??

I don't give you the code, but an algorithm

the_dict = create a dictionary from file A
open an output file C to write the transformed file
open file B for reading
for each line in file B:
    if the line has the form 'League <number>:':
        current_league = extract the number from the line
        write the line to file C
    elif the line has the form 'a = <something>':
        extract the something from the line
        value = the_dict[current_league][something]
        write a line 'b = <value>' in file C
    else:
        write the line to file C unchanged
close file B
close file C

This code will fail with a key error if file B contains a value for a = ... which is not defined in file A.

Can you please check my code below? Unfortunately, it doesn't run due to the error message "KeyError: 0". Thanks a bunch!

f = open('textfile A') 
a = {}
current_league = 0

fout = open ('textfile C', 'w')

text = open ('textfile B', 'r')
for line in text:
    line = line.strip()
    if "League <number>:" in line:
        a[current_league] = {}
        current_league = number
        print ('<number>, fout')
    elif line.lower().startswith(('NEXT LEAGUE', 'a = ')):

    ####I forgot to include 'NEXT LEAGUE' right before
    ####the list of "a =" in textfileB

        my_a = line.split(',')[0]
        value = a[current_league][my_a]
        print ('b = <value>, fout')
    else:
        pass

text.close()
fout.close()

You are putting pseudo code verbatim to your code instead of coding it.

Tony, why do you say this is the verbatim of the pseudocode? It apparently is different from Gribouillis's algorithm and it fits the Python standard and makes sense to me. I'm sure the problem here is that I'm missing 1 or 2 lines or some syntax error that I'm not aware of. I have no real formal training in Python; I'm exploring it out of curiosity in my summer freetime. I'm sure the code is a piece of cake for many of you out there. I hope someone can help me point out specifically what is wrong with my code.

You are using as is the symbols in angle brackets, instead of implementing.

The first wrong thing is line 2 because you must write the code to extract a dictionary like this one from file A instead of an empty dictionary. This first step is required, so concentrate on it. The solution is not somewhere on the net. As you are a python beginner, start with simple tasks. Extracting data from a single file is a way to manipulate and learn the language basics. By the way, if you want a nice dictionary display, use module pprint

from pprint import pprint    
pprint(the_dict)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.