Home > Software design >  How to get elements from a text file and searching them in another text file in python 3?
How to get elements from a text file and searching them in another text file in python 3?

Time:12-15

I have players.txt It is like:

  • Iteration: 1
  • Lloris
  • Kounde
  • Varane
  • Upamecano
  • Hernandez
  • Tchoumeni
  • Rabiot
  • Dembele
  • Griezman
  • Mbappe
  • Giroud
  • Iteration:2
  • Martinez
  • Molina
  • Otamendi
  • Romero
  • Tagliafico
  • Mac Allister
  • De Paul
  • Fernandez
  • Alvarez
  • Messi
  • Lautaro
  • Iteration 3:
  • xxxxx
  • yyyyy
  • zzzzz
  • namenamename
  • name
  • name
  • name

And I have superstarplayers.txt like:

  • Messi
  • Modric
  • Ronaldo
  • Mbappe
  • playername
  • playername
  • playername
  • playername
  • It goes like this, a lot of player name

No blank line in superstarplayer.txt But in players.txt there is a blank line afterward the name of a player line. It is like 1st row is player 2nd row blank 3rd row is player 4th row is blank. One player one blank.

For each iteration, I want to get number of how many players in my iteration. And also I want to get the number of how many players is also exist in superstarplayers.txt How can I do that in python3. I am learning python file operations. (in players.txt in every iteration there are not always 11 players the numbers might different)

I'm learning how to read from a txt file, how to write to a txt file. I have no idea about how to get elements from a text file and searching them in another text file in python 3. How can I do that?

CodePudding user response:

You can do what you are asking with this

def fix_data(lst):
    """ Fixes any blank lines or empty strings"""
    #remove the newline character from the strings if there
    remove_newline = [x.replace("\n","") for x in lst]
    #remove empty strings (your example has them) - also remove whitespace
    remove_empty = [x.strip() for x in remove_newline]
    #return data 
    return remove_empty

#open a text file for reading like this - its wrapped in a fix_data function
with open('/home/lewis/players.txt', 'r') as f:
    lines = fix_data(f.readlines())
#open superstar players -  also wrapped 
with open('/home/lewis/superstar.txt', 'r') as f:
    superstar_lines = fix_data(f.readlines())
    

#create a iteration dictionary.
iteration_store = {}
#create a variable to store the iteration no 
iteration = 0
#create a variable to store the iteration start no 
iteration_start = 0
#create a variable to store unknown player no 
unknown_player = 1
#loop each line and and to dict on each iteration
for line in lines:
    #check if an iteration line
    iteration_start  = 1
    if "Iteration" in line:
        # get the iteration number from the string by splitting the string on 'Iteration:' and taking the last value, then removing white space
        iteration = line.split("Iteration:")[-1].strip()
        #store this value in the dict as a list
        iteration_store[iteration] = []
        #set to zero 
        iteration_start = 0 
    # after the interation line is found then find the player name in every other space
    elif iteration_start % 2 != 0:
        #check if the player is unknown and rename the line
        if line == "" or line is None:
            #set the line
            line = f"Unknown Player {unknown_player}"
            #increment the player number
            unknown_player  = 1 
        #if not an iteration line then store the player name in the dict[list]
        iteration_store[iteration].append(line)
#print the information
print(iteration_store)
#print a new line to seperate the data
print("\n\n")
#loop the newly create dict and print each iteration number and count of players
for k,v in iteration_store.items():
    # check the superstar list for matches
    superstars = [x for x in v if x in superstar_lines]
    #print the information
    print(f"Iteration: {k} - No Players {len(v)} - No Superstars {len(superstars)}")

EDIT:

Changed the code to take the player name from every other line after the "Iteration" line has been found. This will only work if it follows the following format ALWAYS

  • iteration line
  • player name (empty or not)
  • empty string
  • player name (empty or not)

ALSO

If the superstar player is not found, then there must be something that doesnt match like its written messi and Messi as they are different. The code is sound.


Read the codes, research and play with it. Google is your friend.

  • Related