Reputation: 5
I am taking a Python class and I can't figure out a take home quiz. I am using IDLE to write the code.
We have to load a file called names.txt into a list. The file contains the following:
Joe Smith
000000
Jeff Mitchell
xxxxxxx
Benjamin Grant
12346
I need to filter out lines that contain the "xxxxxxx" or numbers. I am attempting to use list comprehension with the following code:
> names = open(r'C:\Users\abcdse\Documents\Python\names1.txt','r')
> names_contents = names.read()
> filtered_names = [n for n in names_contents if n !='xxxxxxx']
> names.close()
> print(filtered_names)
However, when I print the filtered_names output, names are not being filtered and rather than appearing in a dropdown format, they appear like this:
['J', 'o', 'e', ' ', 'S', 'm', 'i', 't', 'h', '\n', '0', '0', '0', '0', '0', '0', '\n', 'J', 'e', 'f', 'f', ' ', 'M', 'i', 't', 'c', 'h', 'e', 'l', 'l', '\n', 'x', 'x', 'x', 'x', 'x', 'x', 'x', '\n', 'B', 'e', 'n', 'j', 'a', 'm', 'i', 'n', ' ', 'G', 'r', 'a', 'n', 't', '\n', '1', '2', '3', '4', '6', '\n']
What am I doing wrong here? Is it possible to filter out both, the "xxxxxxx" and numbers?
Thank you for your support as I get started with code.
Upvotes: 0
Views: 532
Reputation: 1147
You were almost there
names = open(r'C:\Users\abcdsed\Documents\Python\names1.txt','r')
name_contents = names.readlines() # list of lines
filtered_names = [n for n in name_contents if (not n.isnumeric() or n != 'xxxxxxx']
Might want to look things up using your favorite search engine before posting here though. This is a very trivial question.
Upvotes: 2
Reputation: 14114
The values you want to remove
filter_vals = 'xxxxxxx\n'
Read the file
with open('64797525.txt') as f:
out = [i.strip() for i in f.readlines() if i not in filter_vals] # remove what's in the list
print(out)
['Joe Smith', '000000', 'Jeff Mitchell', 'Benjamin Grant', '12346']
Upvotes: 1
Reputation: 866
names_contents
is a string, so you are comparing a string against char in this line of code n !='xxxxxxx'
. So first you have to split the string into list of strings representing each line. Try this
lines = names_contents.split("\n")
filtered_names = [n for n in lines if n !='xxxxxxx']
Upvotes: 2
Reputation: 11342
You can use readlines
to read the data and list comprehension to filter out xxx
ss = '''
Joe Smith
000000
Jeff Mitchell
xxxxxxx
Benjamin Grant
12346
'''.strip()
with open('names.txt','w') as f: f.write(ss) # write data file
###############################
with open('names.txt') as f:
lns = f.readlines()
xx = [ln.strip() for ln in lns if ln.strip() != 'xxxxxxx']
print('\n'.join(xx))
Output
Joe Smith
000000
Jeff Mitchell
Benjamin Grant
12346
Upvotes: 2