I am trying to create a script that will allow a user to enter a number of regular expressions which will pass through an input file and retrieve matches. I am currently using ahocorasick but am getting issues when I try and enter regexed patterns.
I enter a regex into the second raw_input (colour_regex) but receive this error below:
Traceback (most recent call last):
File "PLA_Enrichment_options.py", line 189, in <module>
main()
File "PLA_Enrichment_options.py", line 41, in main
tree.add(regex)
File "build/bdist.linux-x86_64/egg/ahocorasick/__init__.py", line 29, in add
TypeError: argument 1 must be string or read-only buffer, not _sre.SRE_Pattern
file_name = raw_input("What is the filename you wish to enhance? ")
enhanced_name = file_name.replace(".csv", "")
# User regexed input
tree = ahocorasick.KeywordTree()
print ("What regex would you like to use for colour? (Enter 'exit' to move on) ")
colour_regex = raw_input()
regex = re.compile(colour_regex)
while colour_regex != "exit":
tree.add(regex)
tree.make()
print 'Finding colour matches...'
output = open(enhanced_name + '-colour.csv', 'w')
file = open(feed_name, 'r')
for line in iter(file):
id, title, desc, link, image = line.strip('\n').split('\t')
offerString = '|'.join([title.lower(), desc.lower(), link.lower()])
keywords = set()
for match in tree.findall_long(offerString): # find colours
indices = list(match)
keyword = offerString[indices[0]:indices[1]]
if re.search(r'(?<![âêîôûäëïöüà èìòùáéÃóú])\b%s\b(?![âêîôûäëïöüà èìòùáéÃóú])' %(keyword), offerString):
keywords.add(keyword)
if keywords:
output.write('\t'.join([id, '|'.join(keywords), desc, link, image])+'\n')
else:
output.write('\t'.join([id, title, desc, link, image])+'\n')
file.close()
output.close()
Any help/guidance to the right direction would be great.
Thanks
tree = ahocorasick.KeywordTree()
regex = re.compile(colour_regex)
tree.add(regex)
You have passed the wrong type to ahocorasick.KeywordTree.add()
regex is a compiled regular expression object. The type is _sre.SRE_Pattern. If you use the original string instead, you will not get this error.
tree.add(colour_regex)
Also, this will cause an infinite loop. I think you want if instead of while, or put colour_regex = raw_input() inside the loop.
while colour_regex != "exit":
Related
I have a simple login program where I need user to input the correct details to proceed
,This is my code:
email_l = []
pass_l = []
f = open("lab6.txt", "r")
content = f.readlines()
print(content)
for line in content:
s = line.rstrip()
name,email,password,cn,dob,citi,emergency,creditcardnum,creditcardexp,points = s.split(",")
email_l = email
pass_l = password
usergmail = input("enter gmail:")
if usergmail in email_l:
passcode = input("enter password:")
if passcode in pass_l:
print("Login successful! Welcome",name)
display_user()
else:
print("Wrong Password!")
else:
print("wrong gmail")
and this is what contained in the text file
JunYing,jy654#gmail.com,654321,0125489875,12/05/2001,Malaysian,0175987865,2546 4587 5895 5423,21/28,762
john,ok#gmail.com,123456,0165784399,17/7/2003,Malaysian,0124758995,5874 4585 4569 4214,09/25,547
Pepe,tsy#gmail.com,123598,02654898,8/02/2011,American,02165897,5896 4578 5215 4512,07/25,541
I found out it only reads the last line of the file but I'm using a for loop shouldn't it be reading every line in the file?
How can I make it to read every line in the file and make every email that entered into the input is matched with the file.
Due to some rules, only Array can be utilized in the assignment so I can only use array
You should use email_l.append(email) instead of just over writing it since it is a Python List.
Your list variables "email_l" and "pass_l" are overwritten in each loop iteration.
you have to use:
email_l.append(email)
pass_l.append(password)
in order for your code to work.
refer to python data structures documentation to learn more
https://docs.python.org/3/tutorial/datastructures.html
Take care "display_user()" function is not defined in your communicated code. This will raise undefined function errors.
I created this script that could be used as a login, and then created an external text file that has the data 1234 in it, this is attempting to compare the data from the file, but outputs that the two values are different, even though they are the same. Thanks In advance to any help you can give me, the code I used is below:
getUsrName = input("Enter username: ")
file = open("documents/pytho/login/cdat.txt", "r")
lines = file.readlines()
recievedUsrName = lines[1]
file.close()
print(getUsrName)
print(recievedUsrName)
if recievedUsrName == getUsrName:
print("hello")
elif getUsrName != recievedUsrName:
print("bye")
else:
Try it like:
if recievedUsrName.strip() == getUsrName:
...
It must be the trailing newline.
`File = input("Please enter the name for your txt. file: ")
fileName = (File + ".txt")
WRITE = "w"
APPEND = "a"
file = []
name = " "
while name != "DONE" :
name = input("Please enter the guest name (Enter DONE if there is no more names) : ").upper()
fileName.append(name)
fileName.remove("DONE")
print("The guests list in alphabetical order, and it will save in " + fileName + " :")
file.sort()
for U in file :
print(U)
file = open(fileName, mode = WRITE)
file.write(name)
file.close()
print("file written successfully.")
`
I am just practicing to write the file in Python, but something bad happened. Please help me. Thank you.
The code.
The error description.
Here are still some errors about this :
fileName.remove("DONE")
Still showing 'str' error.
THANK YOU
You try to append to string which is not correct in Python, instead try:
filename += 'name'
You're trying to build a list of names. Start with a list:
guests = []
and then append the values provided by your user:
while name is not "Done":
prompt = "Please input the name of the next guest, or 'Done'."
guests.append(input(prompt).upper())
then you can sort that list and write the values to the file. (which you seem to have a handle on)
Appending the guests' names to fileName, or concatenating them onto it, wouldn't make a lot of sense. You'd end up with something like "data.txtJOEBOBJANELINDA" which would do you no good at all.
I'm trying to read in a list of account numbers, then have my program do a search in the appropriate directory for each account number. I want to capture the information from this search, to then split out the file name, date, and time as the output from my program. Currently I'm receiving this error: TypeError: bufsize must be an integer
Here is my code:
def app_files(level):
piv_list_file = raw_input(r"Please enter the full path of the file containing the Pivot ID's you would like to check: ")
piv_id_list = []
proc_out_list = []
input_dir = ''
try:
with open(piv_list_file, 'rbU') as infile:
for line in infile:
line = line.rstrip('\r\n')
piv_id_list.append(line)
except IOError as e:
print 'Unable to open the account number file: %s' % e.strerror
if level == 'p':
input_dir = '[redacted]'
else:
input_dir = '[redacted]'
subprocess.call('cd', input_dir)
for i, e in enumerate(piv_id_list):
proc_out = subprocess.check_output('ls', '-lh', '*CSV*APP*{0}.zip'.format(e))
proc_out_list.append(proc_out)
print(proc_out)
Your subprocess.check_output() function call is wrong. You should provide the complete command as a list (as the first argument). Example -
subprocess.check_output(['ls', '-lh', '*CSV*APP*{0}.zip'.format(e)])
Similar issue with subprocess.call in your code .
I want the text to display the users name if they have entered it before. I have this working in c++ but wanted to practice python. the output will continue to do the "else" statement. I have tried having the if statement search for a string such as "noname" or "empty" and it would still do the else statement.
fr = open('sample.txt','r')
name = fr.read()
fr.close()
if name is None:
fw = open('sample.txt','w')
stuff = raw_input("enter name:")
fw.write(stuff)
fw.close()
else:
print(name)
If you have a blank file without any data in it, f.read() doesn't return None, it returns an empty string.
So rather than do if name is None you could write if name == '' or, to be certain, if name in (None, '').
You might also want to make sure you add a newline character when you write the names to your file, so you should do:
f.write(name + '\n')
for example.
Edit: As Cat Plus Plus mentioned, you can just do if name:, because both None and an empty string will evaluate to False. I just thought it was less clear for this particular question.
Use with to open files, it closes them automtically:
with open('sample.txt','a+') as names: # if file does not exist, "a" will create it
lines = names.read()
if lines: # if file has data
print("Hello {}".format(lines))
else: # else file is empty, ask for name and write name to file
name = raw_input("enter name:")
names.write(name)
To check if the name exists before writing:
with open('sample.txt','a+') as names:
lines = names.read()
name = raw_input("enter name:")
if name in lines:
print("Hello {}".format(name))
else:
names.write(name)