Reputation: 39
I've made a program that's looking through social security numbers and putting them into a textfile either as I wrote them into the program (if the number was valid). or with a comment like the number is too long if the number is too long.
Now i want to be able to "call for them" separatly but i have no idea how to separate the valid numbers from the invalid ones in my textfile.
def showALLnumbers():
print ("all numbers:")
textfile = open("textfile.txt", "r")
data = textfile.read()
print (data)
textfile.close
This is how I call for them all but how do I get just the ones with comments like below, and how do I get the ones without these kind of comments?
if socialsecuritynumber.isdigit()== False:
print (number, "\t", "not only digits")
textfile.write("\n" + socialsecuritynumber + "\t" + "not only digits" )
return False
Sorry for messy text but would appreciate help!
EDIT - this is how it looks in the text file
Upvotes: 0
Views: 100
Reputation:
I'm not terribly sure exactly what your question is. Do you mean that you have a pre-commented batch of social security numbers and you need to pick out the ones with comments? If so, something like this:
comment_list = []
normal_list = []
for line in file:
if len(line) > 9:
comment_list.append(line)
else:
normal_list.append(line)
Upvotes: 0
Reputation: 306
For future reference, you really should save your code in a better format -- CSV is a great format, and Python has intuitive support for it. Being able to load and change things in Excel can be really important!
Regardless, let's work with what you've got right now. What we're going to do is run through each line of the file and check if there's a comment by splitting the line by a space character. CSV works essentially the same way, except it forces requirements on the way lines are split so that it's structured and you won't accidentally split something where you didn't mean to. Notice how if we split by space that if we maybe have an entry like "1234 5678", it won't parse correctly!
Onto the code. From your question, it sounds like you don't want to have any saved data structures, which may run into performance issues if your file gets really big -- I assume we're nowhere near that scale, so that's fine.
def showGoodNumbers():
print ("all good numbers:")
textfile = open("textfile.txt", "r")
for line in textfile.readlines():
split_line = line.split(' ')
if len(split_line) == 1:
print(split_line) # this will print as a tuple
textfile.close
def showBadNumbers():
print ("all bad numbers:")
textfile = open("textfile.txt", "r")
for line in textfile.readlines():
split_line = line.split(' ')
if len(split_line) > 1:
print(split_line) # this will print as a tuple
textfile.close
You could also combine them into one call to create two lists with getters:
good = []
bad = []
def getNumbers():
textfile = open("textfile.txt", "r")
for line in textfile.readlines():
split_line = line.split(' ')
if len(split_line) > 1:
good.append(line)
else:
bad.append(line)
textfile.close
def getBadNumbers():
print("Getting bad numbers")
print("\n".join(bad))
def getGoodNumbers():
print("Getting good numbers")
print("\n".join(good))
Upvotes: 1