I'm working on a project that searches for filenames that match a string. The current working code appends an * to the end of the string and the code properly takes the string and matches it when there's a file extension and/or a number after, that works right. I'm wanting it to ignore things before the initial string as well to check the interior of the file name. For example File A File B.txt will be found by a string search for File A "or File B
Working code:
redeemed = redeemed + "*"
results = [file for file in sounds if re.search(redeemed, file)]
Non-working code:
redeemed = "*" + redeemed + "*"
results = [file for file in sounds if re.search(redeemed, file)]
Returns error:
Task exception was never retrieved
future: <Task finished name='Task-19' coro=<PubSub.__handle_message() done, defined at C:\Users\dmvh1\AppData\Local\Programs\Python\Python310\lib\site-packages\twitchAPI\pubsub.py:293> exception=error('nothing to repeat at position 0')>
Traceback (most recent call last):
File "C:\Users\username\AppData\Local\Programs\Python\Python310\lib\site-packages\twitchAPI\pubsub.py", line 298, in __handle_message
sub(uuid, msg_data)
File "C:\Users\username\Desktop\TwitchSounds\InProgress.py", line 34, in callback_redemptions
results = [file for file in sounds if re.search(redeemed, file)]
File "C:\Users\username\Desktop\TwitchSounds\InProgress.py", line 34, in <listcomp>
results = [file for file in sounds if re.search(redeemed, file)]
File "C:\Users\username\AppData\Local\Programs\Python\Python310\lib\re.py", line 200, in search
return _compile(pattern, flags).search(string)
File "C:\Users\username\AppData\Local\Programs\Python\Python310\lib\re.py", line 303, in _compile
p = sre_compile.compile(pattern, flags)
File "C:\Users\username\AppData\Local\Programs\Python\Python310\lib\sre_compile.py", line 788, in compile
p = sre_parse.parse(p, flags)
File "C:\Users\username\AppData\Local\Programs\Python\Python310\lib\sre_parse.py", line 955, in parse
p = _parse_sub(source, state, flags & SRE_FLAG_VERBOSE, 0)
File "C:\Users\username\AppData\Local\Programs\Python\Python310\lib\sre_parse.py", line 444, in _parse_sub
itemsappend(_parse(source, state, verbose, nested + 1,
File "C:\Users\username\AppData\Local\Programs\Python\Python310\lib\sre_parse.py", line 669, in _parse
raise source.error("nothing to repeat",
re.error: nothing to repeat at position 0
We were concerned the code with redeemed would catch EVERYTHING, but instead it catches nothing. Any ideas on how to implement this search?
If it's just a plain text search, you don't need regex or glob:
results = [file for file in sounds if redeemed in file]
No * or .* needed
If the filename is handled as a String than don't bother with the anyString+*
Just search for whatever you want to find in the string
If my sources are correct the python search() will do the trick for you
"search() functions return a Match object if they find a match of a pattern in a string....search() matches anywhere in the string"
Like this
results = [file for file in sounds if re.search(redeemed, file)]
Related
I had some problems with liking
Cannot detect post media type
I read that I should change one line like_until (span to div) but when I changed that, I got these error:
Traceback (most recent call last): File "c:\Users\lenovo\Desktop\SubDesk\Click Bloom\click_bloom_v1.0.0\main.py", line 72, in <module> session.like_by_tags(['bitcoin'], amount=11) File "C:\Users\lenovo\AppData\Local\Programs\Python\Python39\lib\site-packages\instapy\instapy.py", line 1957, in like_by_tags inappropriate, user_name, is_video, reason, scope = check_link( File "C:\Users\lenovo\AppData\Local\Programs\Python\Python39\lib\site-packages\instapy\like_util.py", line 726, in check_link quash = re.search(dont_likes_regex, image_text, re.IGNORECASE) File "C:\Users\lenovo\AppData\Local\Programs\Python\Python39\lib\re.py", line 201, in search return _compile(pattern, flags).search(string) File "C:\Users\lenovo\AppData\Local\Programs\Python\Python39\lib\re.py", line 304, in _compile p = sre_compile.compile(pattern, flags) File "C:\Users\lenovo\AppData\Local\Programs\Python\Python39\lib\sre_compile.py", line 764, in compile p = sre_parse.parse(p, flags) File "C:\Users\lenovo\AppData\Local\Programs\Python\Python39\lib\sre_parse.py", line 948, in parse p = _parse_sub(source, state, flags & SRE_FLAG_VERBOSE, 0) File "C:\Users\lenovo\AppData\Local\Programs\Python\Python39\lib\sre_parse.py", line 443, in _parse_sub itemsappend(_parse(source, state, verbose, nested + 1, File "C:\Users\lenovo\AppData\Local\Programs\Python\Python39\lib\sre_parse.py", line 671, in _parse raise source.error("multiple repeat", re.error: multiple repeat at position 8
what shoud I do?
Installing instapy from the command line has this error.
To solve that:
Delete any instapy folder in this location:
C:\Users[computer-user]\AppData\Local\Programs\Python\Python39\Lib\site-packages
Go to https://github.com/timgrossmann/InstaPy
Download instaPy-master.zip
Extract the downloaded file and copy the instapy folder from
\InstaPy-master
to
C:\Users[computer-user]\AppData\Local\Programs\Python\Python39\Lib\site-packages
Then run your code
problem solved!
one of my tag contain + and it makes problem
I have a problem when attempting to open a .nc file. For my college work I need to work with some data stored on .nc files, so I decided to give the 'xarray' library a go. The files are located on an OneDrive cloud. When passing the 'open_dataset' function a path that contains non-ASCII characters, the following error occurs:
import xarray as xr
path1 = (r'C:\Users\myname\OneDrive - Prirodoslovno-matematički fakultet'
'\DACCIWA\DATA\Sodar\Save_KIT_CM_20160702.nc')
ds = xr.open_dataset(path1)
Traceback (most recent call last):
File "C:\Users\petar\Desktop\Geofizika\5. Godina\KB - Research opportunity\test.py", line 9, in <module>
ds = xr.open_dataset(path1)
File "C:\Users\petar\Anaconda3\lib\site-packages\xarray\backends\api.py", line 499, in open_dataset
filename_or_obj, group=group, lock=lock, **backend_kwargs
File "C:\Users\petar\Anaconda3\lib\site-packages\xarray\backends\netCDF4_.py", line 389, in open
return cls(manager, group=group, mode=mode, lock=lock, autoclose=autoclose)
File "C:\Users\petar\Anaconda3\lib\site-packages\xarray\backends\netCDF4_.py", line 335, in __init__
self.format = self.ds.data_model
File "C:\Users\petar\Anaconda3\lib\site-packages\xarray\backends\netCDF4_.py", line 398, in ds
return self._acquire()
File "C:\Users\petar\Anaconda3\lib\site-packages\xarray\backends\netCDF4_.py", line 392, in _acquire
with self._manager.acquire_context(needs_lock) as root:
File "C:\Users\petar\Anaconda3\lib\contextlib.py", line 112, in __enter__
return next(self.gen)
File "C:\Users\petar\Anaconda3\lib\site-packages\xarray\backends\file_manager.py", line 183, in acquire_context
file, cached = self._acquire_with_cache_info(needs_lock)
File "C:\Users\petar\Anaconda3\lib\site-packages\xarray\backends\file_manager.py", line 201, in _acquire_with_cache_info
file = self._opener(*self._args, **kwargs)
File "netCDF4\_netCDF4.pyx", line 2135, in netCDF4._netCDF4.Dataset.__init__
File "netCDF4\_netCDF4.pyx", line 1752, in netCDF4._netCDF4._ensure_nc_success
FileNotFoundError: [Errno 2] No such file or directory: b'C:\\Users\\petar\\OneDrive - Prirodoslovno-matemati\xc4\x8dki fakultet\\DACCIWA\\DATA\\Sodar\\Save_KIT_CM_20160702.nc'
I am confused since the file definitely is there (In the code above I replaced my name in the path with "myname", which does not contain non-ASCII characters). At first I thought this had to do something with OneDrive, but I created a folder on it with a path that does not contain non-ASCII characters, and it opens those no problem.
What I tried (although this was really just shotgunning, not familiar with encodings and such):
- input string as raw string (as you do to escape the slashes)
I noticed in the last line that the string path was preceeded by the letter "b", apparently this means that the string is a "byte literal" and can only contain ASCII characters, in which case why does xarray convert/interpret the string as a byte literal? How would I go about opening the file?
Thanks for help!
I am trying to match strings which can be typed from a normal english keyboard.
So, it should include alphabets, digits, and all symbols present on our keyboard.
Corresponding regex : "[a-zA-Z0-9\t ./,<>?;:\"'`!##$%^&*()\[\]{}_+=|\\-]+"
I verfied this regex on regexr.com.
In python, on matching I am getting following error :
>>> a=re.match("+how to block a website in edge",pattern)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\local\Anaconda3-4.1.1-Windows-x86_64\envs\tf_1.2\lib\re.py", line 163, in match
return _compile(pattern, flags).match(string)
File "C:\local\Anaconda3-4.1.1-Windows-x86_64\envs\tf_1.2\lib\re.py", line 293, in _compile
p = sre_compile.compile(pattern, flags)
File "C:\local\Anaconda3-4.1.1-Windows-x86_64\envs\tf_1.2\lib\sre_compile.py", line 536, in compile
p = sre_parse.parse(p, flags)
File "C:\local\Anaconda3-4.1.1-Windows-x86_64\envs\tf_1.2\lib\sre_parse.py", line 829, in parse
p = _parse_sub(source, pattern, 0)
File "C:\local\Anaconda3-4.1.1-Windows-x86_64\envs\tf_1.2\lib\sre_parse.py", line 437, in _parse_sub
itemsappend(_parse(source, state, nested + 1))
File "C:\local\Anaconda3-4.1.1-Windows-x86_64\envs\tf_1.2\lib\sre_parse.py", line 638, in _parse
source.tell() - here + len(this))
sre_constants.error: nothing to repeat at position 0
This error message is not about position of arguments. Yes, in question above they are not in the right order, but this is only half of problem.
I've got this problem once when i had something like this:
re.search('**myword', '/path/to/**myword')
I wanted to get '**' automatically so i did not wanted to write '\' manually somewhere. For this cause there is re.escape() function. This is the right code:
re.search(re.escape('**myword'), '/path/to/**myword')
The problem here is that special character placed after the beginning of line.
You have your arguments for re.match backward: it should be
re.match(pattern, "+how to block a website in edge")
Im trying to read a file line by line. I want to replace key with value if found in the dictionary and write the contents to the new file. Here is the logic:
fout = open(output_file,"w+")
with open(input_file, 'r') as fin:
for line in fin:
for key in sorted(Db):
if re.match(key,line):
line = re.sub(key,Db[key],line) ## line 246
fout.write(line)
break
else:
fout.write(line)
Whenever i try to run this file, I'm getting the following tracebacks:
Traceback (most recent call last):
File "final.py", line 246, in <module>
if re.match(key,line):
File "c:\Python33\lib\re.py", line 156, in match
return _compile(pattern, flags).match(string)
File "c:\Python33\lib\functools.py", line 258, in wrapper
result = user_function(*args, **kwds)
File "c:\Python33\lib\re.py", line 274, in _compile
return sre_compile.compile(pattern, flags)
File "c:\Python33\lib\sre_compile.py", line 493, in compile
p = sre_parse.parse(p, flags)
File "c:\Python33\lib\sre_parse.py", line 724, in parse
p = _parse_sub(source, pattern, 0)
File "c:\Python33\lib\sre_parse.py", line 347, in _parse_sub
itemsappend(_parse(source, state))
File "c:\Python33\lib\sre_parse.py", line 552, in _parse
raise error("nothing to repeat")
sre_constants.error: nothing to repeat
Kindly let me know if I'm missing something. Thanks in advance.
Thanks,
Anand
I think you should try and debug this problem yourself. Here is what I would do.
add a print statement in your script before line 246:
print key,
print Db[key]
print line
Depending on the output, take action.
To test what would work, you can use the python interpreter.
Assuming you get out of the print above:
key
foo
key 123
you can test it:
line = 'key 123'
re.sub('key', 'foo', line)
'foo 123'
In this case it works. I'm sure you'll soon find out what the problem is. Good luck!
I am trying this code from here docs
class Form(Form):
image = FileField(u'Image File', validators=[Regexp(u'^[^/\\]\.jpg$')])
def validate_image(form, field):
if field.data:
field.data = re.sub(r'[^a-z0-9_.-]', '_', field.data)
Here is the error:
Traceback (most recent call last):
File "tornadoexample2-1.py", line 111, in <module>
class Form(Form):
File "tornadoexample2-1.py", line 119, in Form
image = FileField(u'Image File', validators=[Regexp(u'^[^/\\]\.jpg$')])
File "/usr/local/lib/python2.7/dist-packages/wtforms/validators.py", line 256, in __init__
regex = re.compile(regex, flags)
File "/usr/lib/python2.7/re.py", line 190, in compile
return _compile(pattern, flags)
File "/usr/lib/python2.7/re.py", line 242, in _compile
raise error, v # invalid expression
sre_constants.error: unexpected end of regular expression
Any idea about what the problem?
The regexp in Regexp(u'^[^/\\]\.jpg$') is not quite good.
Try running this, you will get the same exception:
import re
re.compile(u'^[^/\\]\.jpg$')
You need to escape each \ slash twice inside the [] brackets.
So you can rewrite it as u'^[^/\\\\]\.jpg$' or as a raw string ur'^[^/\\]\.jpg$'.
Hope this helps.