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!
Related
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)]
Rather a bug report with possible fix. I'm using version 3.0.9.
One of the files I need to handle has a problem with one of the images. When I open it with libreoffice, I see placeholder instead of an image. But when I open it with load_workbook(), an exception occurs:
Traceback (most recent call last):
File "/home/pooh/work/isaac_choi/./1.py", line 5, in <module>
wb=load_workbook('pritelli/FW21 WOMAN 27.09.21.xlsx')
File "/home/pooh/venv39/lib/python3.9/site-packages/openpyxl/reader/excel.py", line 317, in load_workbook
reader.read()
File "/home/pooh/venv39/lib/python3.9/site-packages/openpyxl/reader/excel.py", line 282, in read
self.read_worksheets()
File "/home/pooh/venv39/lib/python3.9/site-packages/openpyxl/reader/excel.py", line 257, in read_worksheets
charts, images = find_images(self.archive, rel.target)
File "/home/pooh/venv39/lib/python3.9/site-packages/openpyxl/reader/drawings.py", line 52, in find_images
image = Image(BytesIO(archive.read(dep.target)))
File "/usr/lib/python3.9/zipfile.py", line 1463, in read
with self.open(name, "r", pwd) as fp:
File "/usr/lib/python3.9/zipfile.py", line 1502, in open
zinfo = self.getinfo(name)
File "/usr/lib/python3.9/zipfile.py", line 1429, in getinfo
raise KeyError(
KeyError: "There is no item named 'xl/drawings/NULL' in the archive"
I think KeyError can be handled right after OSError (line 53), and just continue iterating in this case:
except KeyError:
warn('Missing image')
continue
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
To get more hands-on experience I wanted to try a project word count.
Here is the sample data which I have.
The United Nations (UN) is an intergovernmental organisation
established on 24 October 1945 to promote international cooperation. A
replacement for the ineffective League of Nations, the organisation
was created following World War II to prevent another such conflict.
[...]
and i used the following python code to get my result
from mrjob.job import MRJob
from mrjob.step import MRStep
class MovieRatings(MRJob):
def steps(self):
return [
MRStep(mapper=self.mapper_get_ratings,
reducer=self.reducer_count_ratings),
]
def mapper_get_ratings(self, _, line):
(word) = line.split(' ')
yield word, 1
def reducer_count_ratings(self, key, values):
yield Key, sum(values)
if __name__ == '__main__':
MovieRatings.run()
i am getting the following error with Python 2
[root#localhost Desktop]# python RatingsBreakdown.py UN.txt
Traceback (most recent call last):
File "RatingsBreakdown.py", line 1, in <module>
from mrjob.job import MRJob
File "/usr/lib/python2.6/site-packages/mrjob/job.py", line 1106
for k, v in unfiltered_jobconf.items() if v is not None
^
SyntaxError: invalid syntax
Also Python 3
[root#localhost Desktop]# python3 RatingsBreakdown.py UN.txt
No configs found; falling back on auto-configuration
No configs specified for inline runner
Running step 1 of 2...
Creating temp directory /tmp/RatingsBreakdown.training.20171128.083536.602598
Error while reading from /tmp/RatingsBreakdown.training.20171128.083536.602598/step/000/mapper/00000/input:
Traceback (most recent call last):
File "RatingsBreakdown.py", line 25, in <module>
RatingsBreakdown.run()
File "/usr/lib/python3.4/site-packages/mrjob/job.py", line 424, in run
mr_job.execute()
File "/usr/lib/python3.4/site-packages/mrjob/job.py", line 445, in execute
super(MRJob, self).execute()
File "/usr/lib/python3.4/site-packages/mrjob/launch.py", line 185, in execute
self.run_job()
File "/usr/lib/python3.4/site-packages/mrjob/launch.py", line 233, in run_job
runner.run()
File "/usr/lib/python3.4/site-packages/mrjob/runner.py", line 511, in run
self._run()
File "/usr/lib/python3.4/site-packages/mrjob/sim.py", line 144, in _run
self._run_mappers_and_combiners(step_num, map_splits)
File "/usr/lib/python3.4/site-packages/mrjob/sim.py", line 185, in _run_mappers_and_combiners
for task_num, map_split in enumerate(map_splits)
File "/usr/lib/python3.4/site-packages/mrjob/sim.py", line 120, in _run_multiple
func()
File "/usr/lib/python3.4/site-packages/mrjob/sim.py", line 662, in _run_mapper_and_combiner
run_mapper()
File "/usr/lib/python3.4/site-packages/mrjob/sim.py", line 685, in _run_task
stdin, stdout, stderr, wd, env)
File "/usr/lib/python3.4/site-packages/mrjob/inline.py", line 92, in invoke_task
task.execute()
File "/usr/lib/python3.4/site-packages/mrjob/job.py", line 433, in execute
self.run_mapper(self.options.step_num)
File "/usr/lib/python3.4/site-packages/mrjob/job.py", line 517, in run_mapper
for out_key, out_value in mapper(key, value) or ():
File "RatingsBreakdown.py", line 13, in mapper_get_ratings
(userID, movieID, rating, timestamp) = line.split('\t')
ValueError: need more than 1 value to unpack
Also with my MovieRatings
[root#localhost Desktop]# python3 MovieRatings.py UN.txt
No configs found; falling back on auto-configuration
No configs specified for inline runner
Running step 1 of 1...
Creating temp directory /tmp/MovieRatings.training.20171128.083635.368889
Error while reading from /tmp/MovieRatings.training.20171128.083635.368889/step/000/reducer/00000/input:
Traceback (most recent call last):
File "MovieRatings.py", line 20, in <module>
MovieRatings.run()
File "/usr/lib/python3.4/site-packages/mrjob/job.py", line 424, in run
mr_job.execute()
File "/usr/lib/python3.4/site-packages/mrjob/job.py", line 445, in execute
super(MRJob, self).execute()
File "/usr/lib/python3.4/site-packages/mrjob/launch.py", line 185, in execute
self.run_job()
File "/usr/lib/python3.4/site-packages/mrjob/launch.py", line 233, in run_job
runner.run()
File "/usr/lib/python3.4/site-packages/mrjob/runner.py", line 511, in run
self._run()
File "/usr/lib/python3.4/site-packages/mrjob/sim.py", line 150, in _run
self._run_reducers(step_num, num_reducer_tasks)
File "/usr/lib/python3.4/site-packages/mrjob/sim.py", line 246, in _run_reducers
for task_num in range(num_reducer_tasks)
File "/usr/lib/python3.4/site-packages/mrjob/sim.py", line 120, in _run_multiple
func()
File "/usr/lib/python3.4/site-packages/mrjob/sim.py", line 685, in _run_task
stdin, stdout, stderr, wd, env)
File "/usr/lib/python3.4/site-packages/mrjob/inline.py", line 92, in invoke_task
task.execute()
File "/usr/lib/python3.4/site-packages/mrjob/job.py", line 439, in execute
self.run_reducer(self.options.step_num)
File "/usr/lib/python3.4/site-packages/mrjob/job.py", line 560, in run_reducer
for out_key, out_value in reducer(key, values) or ():
File "MovieRatings.py", line 17, in reducer_count_ratings
yield Key, sum(values)
NameError: name 'Key' is not defined
I would like to solve the error and understand what ny mistake is.
Seems like this library only works in Python3
File "RatingsBreakdown.py", line 13, in mapper_get_ratings
(userID, movieID, rating, timestamp) = line.split('\t')
ValueError: need more than 1 value to unpack
First, you ran RatingsBreakdown.py... Also, your shown input contains no tabs and you have tried to extract 4 columns. Not really clear what you expected here.
File "MovieRatings.py", line 17, in reducer_count_ratings
yield Key, sum(values)
NameError: name 'Key' is not defined
Self explanatory... your variable is lowercase key
You are trying the examples in this course (link) right?
According to this issue, mrjob has dropped for Python 2.6.
How I fixed it is installing Python 2.7 (refernece) on the CentOS the VM is using. Then set up pip (reference) and install mrjob again.
Everything works now by running this:
python2.7 RatingsBreakdown.py u.data
I worked on the same but without using steps function. It worked.
from mrjob.job import MRJob
class wordcount(MRJob):
def mapper(self, _, line):
(word) = line.split(' ')
yield word, 1
def reducer(self,x,count):
yield x,sum(count)
if __name__ == '__main__':
wordcount.run()
I'm trying to build a python function with web.py and SQLite that will allow users to search for a given string within a description field and will return all matching results.
Right now I've gotten to the below function, which works but only if the input is an exact match.
def getItem(params, max_display):
query_string = 'SELECT * FROM items WHERE 1=1'
description = params['description']
if params['description']:
query_string = query_string + ' AND description LIKE $description'
result = query(query_string, {
'description': params['description']
I've tried to implement this feature with LIKE "%$description%"' , however I keep getting the below web.py error.
Traceback (most recent call last):
File "lib/web/wsgiserver/__init__.py", line 1245, in communicate
req.respond()
File "lib/web/wsgiserver/__init__.py", line 775, in respond
self.server.gateway(self).respond()
File "lib/web/wsgiserver/__init__.py", line 2018, in respond
response = self.req.server.wsgi_app(self.env, self.start_response)
File "lib/web/httpserver.py", line 306, in __call__
return self.app(environ, xstart_response)
File "lib/web/httpserver.py", line 274, in __call__
return self.app(environ, start_response)
File "lib/web/application.py", line 279, in wsgi
result = self.handle_with_processors()
File "lib/web/application.py", line 249, in handle_with_processors
return process(self.processors)
File "lib/web/application.py", line 246, in process
raise self.internalerror()
File "lib/web/application.py", line 478, in internalerror
return debugerror.debugerror()
File "lib/web/debugerror.py", line 305, in debugerror
return web._InternalError(djangoerror())
File "lib/web/debugerror.py", line 290, in djangoerror
djangoerror_r = Template(djangoerror_t, filename=__file__, filter=websafe)
File "lib/web/template.py", line 846, in __init__
code = self.compile_template(text, filename)
File "lib/web/template.py", line 926, in compile_template
ast = compiler.parse(code)
File "/Users/sokeefe/homebrew/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/compiler/transformer.py", line 51, in parse
return Transformer().parsesuite(buf)
File "/Users/sokeefe/homebrew/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/compiler/transformer.py", line 128, in parsesuite
return self.transform(parser.suite(text))
AttributeError: 'module' object has no attribute 'suite'
Any thoughts on what might be going wrong with this function?
Thanks in advance!
What do you think is going on with parser.py?
Here is the relevant portion of the error message:
File
"/Users/sokeefe/homebrew/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/compiler/transformer.py",
line 128, in parsesuite
return self.transform(parser.suite(text)) AttributeError: 'module' object has no attribute 'suite'
So, somewhere there is a file called parser.py, which defines a function called suite(), which is used by some library code that executes when your program executes. But because you named one of your files parser.py, when the library code executes, python searches for a file named parser.py, and python found your file first, and there was no function named suite() in your file.