How do I solve this IndexError inside a for Loop? - python

I have been playing around with Python for over a year now and written several automation codes which I use on a daily basis. I have been writing this auto typer for Python, here is the code:
import pyautogui as pt
from time import sleep
empty_file = "C:\\Users\\Lucas\\Desktop\\PycharmProjects\\Automate\\main\\screenshots\\empty_file.png"
text_write = "C:\\Users\\Lucas\\Desktop\\PycharmProjects\\Automate\\main\\text_write.txt"
with open(text_write, 'r') as f:
text = f.read()
sentence = text.split("\n")
position0 = pt.locateOnScreen(empty_file, confidence=.8)
x = position0[0]
y = position0[1]
pt.moveTo(x, y, duration=.05)
pt.leftClick()
def post_text():
pt.moveTo(x-370, y+95, duration=.1)
for lines in range(len(text)):
pt.typewrite(str(sentence[lines],) + "\n", interval=.01)
with pt.hold('shift'):
pt.press('tab', presses=5)
sleep(2)
post_text()
The code completely works but at the end instead of the code breaking it gives me this error:
C:\Users\Lucas\PycharmProjects\wechat_bot\venv\Scripts\python.exe C:/Users/Lucas/Desktop/PycharmProjects/Automate/main/auto_typer.py
Traceback (most recent call last):
File "C:\Users\Lucas\Desktop\PycharmProjects\Automate\main\auto_typer.py", line 26, in <module>
post_text()
File "C:\Users\Lucas\Desktop\PycharmProjects\Automate\main\auto_typer.py", line 20, in post_text
pt.typewrite(str(sentence[lines],) + "\n", interval=.01)
IndexError: list index out of range
Process finished with exit code 1
I suspect the issue has to do specifically with the line:
str(sentence[lines],
I haven't found a solution yet. Am I supposed to be using len() or would and if else statement be better?

The problem is that you are doing
for lines in range(len(text)):
but then you later do
sentence[lines]
This will only work text is shorter than or the same length as sentence. But there is no such guarantee in your code.
Instead, you should do
for lines in range(len(sentence)):
Or better yet loop over the lines without indexes:
for line in sentence:
And then you can just do line instead of sentence[lines].

Related

How to complete for loop with pdfplumber?

Problem
I was following this tutorial https://www.youtube.com/watch?v=eTz3VZmNPSE&list=PLxEus0qxF0wciRWRHIRck51EJRiQyiwZT&index=16
when the code has returned my this error.
Goal
I need to scrape a pdf that looks like this (I wanted to attach the pdf but I do not know how):
170001WO01
English (US) into Arabic (DZ)
Trans./Edit/Proof. 22.117,00 Words 1,350 29.857,95
TM - Fuzzy Match 2.941,00 Words 0,500 1.470,50
TM - Exact Match 353,00 Words 0,100 35,30
Approach
I am following the tutorial aforementioned with pdfplumber.
import re
import pdfplumber
import PyPDF2
import pandas as pd
from collections import namedtuple
ap = open('test.pdf', 'rb')
I name the column of the dataframe that I want as a final product.
Serv = namedtuple('Serv', 'case_number language num_trans num_fuzzy num_exact')
Issues
I have 5 different lines compared to the tutorial example which has 2.
case_li = re.compile(r'(\d{6}\w{2}\d{2})')
language_li = re.compile(r'(nglish \(US\) into )(.*)')
trans_li = re.compile(r'(Trans./Edit/Proof. )(\d{2}\.\d{3})')
fuzzy_li = re.compile(r'(TM - Fuzzy Match )(\d{1}\.\d{3})')
exact_li = re.compile(r'(M - Exact Match )(\d{3})')
Issue
When I introduce the third line in the code, I got an error which I do not know. I have modified the code as 2e0byo suggested but I still get an error.
This is the new code:
line_items = []
with pdfplumber.open(ap) as pdf:
page = pdf.pages
for page in pdf.pages:
text = page.extract_text()
for line in text.split('\n'):
line = case_li.search(line)
if line:
case_number = line
line = language_li.search(line)
if line:
language = line.group(2)
line = trans_li.search(line)
if line:
num_trans = line.group(2)
line = fuzzy_li.search(line)
if line:
num_fuzzy = line.group(2)
line = exact_li.search(line)
if line:
num_exact = line.group(2)
line_items.append(Serv(case_number, language, num_trans, num_fuzzy, num_exact))```
---------------------------------------------------------------------------
and this is the new error:
TypeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_13992/1572426536.py in <module>
10 case_number = line
11
---> 12 line = language_li.search(line)
13 if line:
14 language = line.group(2)
TypeError: expected string or bytes-like object
TypeError: expected string or bytes-like object
# GOAL
It would be to append the lines to line_items and eventually
df = pd.DataFrame(line_items)
You have reassigned line, here:
for line in text.split("\n"):
# line is a str (the line)
line = language_li.search(line)
# line is no longer a str, but the result of a re.search
so line is no longer the text line, but the result of that match. Thus trans_li.search(line) is not searching the line you thought it was.
To fix your code, adopt a consistent pattern:
for line in text.split("\n"):
match = language_li.search(line)
# line is still a str (the line)
# match is the result of re.search
if match:
do_something(match.groups())
...
# line is *still* a str
match = trans_li.search(line):
if match:
...
For completeness' sake, with the dreaded walrus operator you can now write this:
if match := language_li.search(line) is not None:
do_something(match.groups())
Which I briefly thought was neater, but now think ugly. I fully expect to get downvoted just for mentioning the walrus operator. (If you look at the edit history of this post you will see that I have even forgotten how to use it and wrote it backwards first.)
PS: you may wish to read up on variable scope in python, although no language I know would allow this particular scope collision (overwriting a loop variable within the loop). Incidentally doing this kind of thing by mistake is why conventionally we avoid similarly-named variables (like line and Line) and go with things like line and match instead.

How do I sort Python IndexError:List Index out of range when reading and writing with files

I'm working on a game in Python and at the end, scores are written to a file and then the top 5 scores are extracted from the file. This usually works perfectly fine but once I reset the high scores I get an Index error saying "the list index is out of range"
Traceback (most recent call last):
File "/home/leo/Documents/Python/infinitest/infinitest.py", line 172, in <module>
scoreboard()
File "/home/leo/Documents/Python/infinitest/infinitest.py", line 147, in scoreboard
print("{0[0]} : {1[0]}\n{0[1]} : {1[1]}\n{0[2]} : {1[2]}\n{0[3]} : {1[3]}\n{0[4]} : {1[4]}".format(scores,names))
IndexError: list index out of range
How would I fix this
def scoreboard():
c = add_up1(False)
d = add_up2(False)
with open("/home/leo/Documents/Python/infinitest/hi2.txt", "a+") as leaders:
leaders.write('{},{}\n'.format(c,name1))
leaders.write('{},{}\n'.format(d,name2))
line=leaders.readline()
dic={}
for line in leaders:
data = line.split(",")
dic[int(data[0])] = data[1]
dic1={}
for key in sorted(dic.keys()):
dic1[key]=dic[key]
scores=list(dic1.keys())
names=list(dic1.values())
names =names[::-1]
scores= scores[::-1]
print("{0[0]} : {1[0]}\n{0[1]} : {1[1]}\n{0[2]} :{1[2]}\n{0[3]} : {1[3]}\n{0[4]} : {1[4]}".format(scores,names))
In the external file, it is formatted so there is the score, followed by a comma, followed by a
username. For example:
100,exampleuser
The add_up functions are fine and just return the total score.
I've tried to add placeholder scores to fix the problem, like
1,Placeholder1
2,Placeholder2
3,Placeholder3
4,Placeholder4
5,Placeholder5
and this sometimes work but now is not working again.
After writing to the file its position is at the end - you can see that with leaders.tell(). When you start reading, the for loop exits immediately because there are no more lines and dic remains empty. Later, scores and names are empty so you get an IndexError when you try to access items.
Before starting to read the file set it's position back to the beginning - if there is a header that you don't want skip the first line:
...
leaders.seek(0)
#_ = next(leaders) # skip header
for line in leaders:
data = line.split(",")
dic[int(data[0])] = data[1]

Please correct my code Python

I am trying to read from a file and return solutions based on the problem that the user inputs. I have saved the text file in the same location, that is not an issue. At the moment, the program just crashes when I run it and type a problem eg "screen".
Code
file = open("solutions.txt", 'r')
advice = []
read = file.readlines()
file.close()
print (read)
for i in file:
indword = i.strip()
advice.append (indword)
lst = ("screen","unresponsive","frozen","audio")
favcol = input("What is your problem? ")
probs = []
for col in lst:
if col in lst:
probs.append(col)
for line in probs:
for solution in advice:
if line in solution:
print(solution)
The text file called "solutions.txt" holds the following info:
screen: Take the phone to a repair shop where they can replace the damaged screen.
unresponsive: Try to restart the phone by holding the power button for at least 4 seconds.
frozen: Try to restart the phone by holding the power button for at least 4 seconds.
audio: If the audio or sound doesnt work, go to the nearest repair shop to fix it.
Your question reminds me a lot of my learning, so I will try give an answer to expand on your learning with lots of print statements to consider how it works carefully. It's not the most efficient or stable approach but hopefully of some use to you to move forwards.
print "LOADING RAW DATA"
solution_dictionary = {}
with open('solutions.txt', 'r') as infile:
for line in infile:
dict_key, solution = line.split(':')
print "Dictionary 'key' is: ", dict_key
print "Corresponding solution is: ", solution
solution_dictionary[dict_key] = solution.strip('\n')
print '\n'
print 'Final dictionary is:', '\n'
print solution_dictionary
print '\n'
print 'FINISHED LOADING RAW DATA'
solved = False
while not solved: # Will keep looping as long as solved == False
issue = raw_input('What is your problem? ')
solution = solution_dictionary.get(issue)
""" If we can find the 'issue' in the dictionary then 'solution' will have
some kind of value (considered 'True'), otherwise 'None' is returned which
is considered 'False'."""
if solution:
print solution
solved = True
else:
print ("Sorry, no answer found. Valid issues are 'frozen', "
"'screen' 'audio' or 'unresponsive'")
want_to_exit = raw_input('Want to exit? Y or N? ')
if want_to_exit == 'Y':
solved = True
else:
pass
Other points:
- don't use 'file' as a variable name anywhere. It's a python built-in and can cause some weird behaviour that you'll struggle to debug https://docs.python.org/2/library/functions.html
- If you get an error, don't say "crashes", you should provide some form of traceback e.g.:
a = "hello" + 2
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-1-6f5e94f8cf44> in <module>()
----> 1 a = "hello" + 2
TypeError: cannot concatenate 'str' and 'int' objects
your question title will get you down-votes unless you are specific about the problem. "help me do something" is unlikely to get a positive response because the error is ambiguous, there's no sign of Googling the errors (and why the results didn't work) and it's unlikely to be of any help to anyone else in the future.
Best of luck :)
When I change the line "for i in file:" to "for i in read:" everything works well.
To output only the line starting with "screen" just forget the probs variable and change the last for statement to
for line in advice:
if line.startswith( favcol ) :
print line
break
For the startswith() function refer to https://docs.python.org/2/library/stdtypes.html#str.startswith
And: the advices of roganjosh are helpfull. Particularly the one "please don't use python keywords (e.g. file) as variable names". I spent hours of debugging with some bugs like "file = ..." or "dict = ...".

Wordnet synset - strange list index out of range Error

I started working with nltk and I am attempting to generate a function that would allow me to pass in an adjective, extract the first synset from wordnet and print it alongside its antonym. Her is my code:
def placementOperator(wordItem):
wordnet_lemmatizer = WordNetLemmatizer()
placementItem = wordnet_lemmatizer.lemmatize(wordItem,'a')
print("The placementItem is: " + placementItem)
iterationSet = wn.synsets(placementItem, 'a')
if iterationSet[0]:
print(" This is the SS NAME : " + iterationSet[0].name())
for j in iterationSet[0].lemmas():
print(" This is the LEMMAAAA: " + j.name())
if j.antonyms():
print(" This is the RElATIONSHIP " + j.name(), j.antonyms()[0].name())
else: print(" _______> NO ANTONYM!")
else: pass
I am almost there, except that my interpreter throws a 'list out of range' exception. I know that I can't call a list position that doesn't exist and I know that this error occurs when one tries to do so. But since I am explicitly testing for this with if iterationSet[0] I am not sure how I am ending up with the error anyways.
Any advice would be highly appreciated.
Her is the error:
Traceback (most recent call last):
File "C:/Users/Admin/PycharmProjects/momely/associate/associate.py", line 57, in <module> preProcessor(0)
File "C:/Users/Admin/PycharmProjects/momely/associate/associate.py", line 54, in preProcessor placementOperator(each_element[0])
File "C:/Users/Admin/PycharmProjects/momely/associate/associate.py", line 31, in placementOperator if iterationSet[0]:
IndexError: list index out of range
Most likely, wn.synsets(placementItem, 'a') returned you an empty list. This can happen if placementItem isn't in wordnet.
Therefore, when you did iterationSet[0], it throws an out of range exception. Instead, you can change your check to be :
if iterationSet:
print( ....
....
instead of
if iterationSet[0]:
print(...

Python, iterating through a list to perform a search

I hope someone can point out where I have gone wrong. I am looking to iterate through the 'mylist' list to grab the first entry and use that first entry as a search string, then perform a search and gather particular information once the string is found and post it to an Excel worksheet. Then I am hoping to iterate to the next 'mylist' entry and perform another search. The first iteration performs ok, but with the second iteration of the loop I get the following CMD window error...
2014 Apr 25 09:43:42.080 INFORMATION FOR A
14.01
Traceback (most recent call last):
File "C:\TEST.py", line 362, in <module>
duta()
File "C:\TEST.py", line 128, in duta
if split[10] == 'A':
IndexError: list index out of range
Exception RuntimeError: RuntimeError('sys.meta_path must be a list of
import hooks',) in <bound method Workbook.__del__ of
<xlsxwriter.workbook.Workbook object at 0x0238C310>> ignored
Here's my code...
for root, subFolders, files in chain.from_iterable(os.walk(path) for path in paths):
for filename in files:
if filename.endswith('.txt'):
with open(os.path.join(root, filename), 'r') as fBMA:
searchlinesBMA = fBMA.readlines()
fBMA.close()
row_numBMAA+=1
num = 1
b = 1
print len(mylist)
print (mylist[num])
while b<len(mylist):
for i, line in enumerate(searchlinesBMA):
for word in [mylist[num]]:
if word in line:
keylineBMA = searchlinesBMA[i-2]
Rline = searchlinesBMA[i+10]
Rline = re.sub('[()]', '', Rline)
valueR = Rline.split()
split = keylineBMA.split()
if split[6] == 'A':
print keylineBMA
print valueR[3]
worksheetFILTERA.write(row_numBMAA,3,valueR[3], decimal_format)
row_numBMAA+=1
break
num+=1
b=+1
Any ideas as to what I am doing wrong? Is my loop out of position, or am I not inputting the correct list pointer?
Thanks,
MikG
In my experience, this error is related to garbage collecting out of order. I saw it once when I was debugging code where someone was writing to files in a __del__ method. (Bad idea). I'm pretty sure you're getting the error because you're closing the file inside a with: block, which does the open and close for you.
On the second run, you got split = keylineBMA.split() with a result shorter than you expected. You try to access index 10 which is outside the list.

Categories