简体   繁体   中英

Python: check if a variable exists in a .txt file

i have 3 files in my folder. (pythonapp.py, numbers.txt, usednumbers.txt). So basically my app grabs a random string from (numbers.txt) and saves it to a variable. Then it writes the variable to (usednumbers.txt). But the problem is i don't want to get any duplicate strings written to (usednumbers.txt). So i'd like my app to check whenever it grabs a new random string from (numbers.txt), that if it has been already used in the past (so if it exists in usednumbers.txt)

#imports-------------------------------
import random

#variables-----------------------------
line = random.choice(open('numbers.txt').readlines()) #get a random string
textfile = open("usednumbers.txt", "a") #open usednumbers txt file

#main----------------------------------
#here: need to check if our selected random variable "line"
#is in txt file "usednumbers.txt", if it exists there, we get a new random number
#if it doesn't exist there, we continue and write the variable there (below)

textfile.write(line) #store the number we are going to use
textfile.close() #so in future we avoid using this number again

You can modify your code little checking again and again into the file will be overkill and reading is time expensive.

So you can read number file and filter the number which are not present in usednumber and choose a random number from it.

#imports-------------------------------
import random

#variables-----------------------------
numbers = open('numbers.txt').read().splitlines() #get a random string

# alreday in usednumbes 
already_in_file = set([x for x in open("usednumbers.txt", "r").read().splitlines()])

# filtering only those number swhich are not in number.txt
numbers = [x for x in numbers if x not in already_in_file]

#choose random number
if len(numbers) >0 :  
  line = random.choice(numbers)

  #main----------------------------------
  textfile = open("usednumbers.txt", "a") #open usednumbers txt file
  textfile.writelines(str(line)+"\n") #store the number we are going to use
  textfile.close() #so in future we avoid using this number again

You can check if the chosen line is in the lines of the text file.

#imports-------------------------------
import random

#variables-----------------------------
line = random.choice(open('numbers.txt').readlines()) #get a random string
textfile = open("usednumbers.txt", "a") #open usednumbers txt file

#main----------------------------------
#here: need to check if our selected random variable "line"
#is in txt file "usednumbers.txt", if it exists there, we get a new random number
#if it doesn't exist there, we continue and write the variable there (below)
while(line in open('usednumbers.txt').readlines()):
    line = random.choice(open('numbers.txt').readlines()) #get a random string

textfile.write(line) #store the number we are going to use
textfile.close() #so in future we avoid using this number again

Update:

Not the most time performant solution.

See the solution provided by @Suryaveer Singh for a more optimized solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM