Issue
This is the part of the code, and indicate the line that gives an error with a comment #THIS IS THE LINE. in this case I want the list to store the line number of the txt, but it appears to me that this index is not even initialized.
import re, random, os
from os import remove
from unicodedata import normalize
def name_and_img_identificator(input_text, text):
#Quita acentos y demas diacríticos excepto la ñ
input_text = re.sub(
r"([^n\u0300-\u036f]|n(?!\u0303(?![\u0300-\u036f])))[\u0300-\u036f]+", r"\1",
normalize("NFD", input_text), 0, re.I
)
input_text = normalize( 'NFC', input_text) # -> NFC
input_text_to_check = input_text.lower() #Convierte a minuscula todo
#Inicializo listas vacias
persons_names = []
persons_identifier_img = []
if (os.path.isfile('data_association/persons_names_file.txt')):
persons_names_file_path = 'data_association/persons_names_file.txt'
else:
open('data_association/persons_names_file.txt', "w")
persons_names_file_path = 'data_association/persons_names_file.txt'
#Lo usare como un dato asociado
if (os.path.isfile('data_association/persons_identifier_img_file.txt')):
persons_identifier_img_file_path = 'data_association/persons_identifier_img_file.txt'
else:
open('data_association/persons_identifier_img_file.txt', "w")
persons_identifier_img_file_path = 'data_association/persons_identifier_img_file.txt'
with open(persons_names_file_path, "r", encoding="utf-8") as textfile:
#positive_words.extend(textfile.readlines() + [""])
persons_names.extend(textfile.readlines())
print(persons_names)
with open(persons_identifier_img_file_path, "r", encoding="utf-8") as textfile:
#negative_words.extend(textfile.readlines() + [""])
persons_identifier_img.extend(textfile.readlines())
print(persons_identifier_img)
regex_patron_00 = r"\s*\¿?(?:mi nombre es|me llamo|me llaman|me conocen como)\s*(:|)\s*((?:\w+\s*)+)\s*\??"
m = re.search(regex_patron_00, input_text_to_check, re.IGNORECASE) #Con esto valido la regex haber si entra o no en el bloque de code
if m:
print("A")
person_name = m.group()
person_name = person_name.strip()
#persons_identifier_img = persons_identifier_img.strip()
persons_identifier_img = ""
person_name_check = person_name + "\n"
persons_identifier_img_check = persons_identifier_img + "\n"
redefinition = False
both_coincidences = False
num_linea = None
#try:
if(1 == 1):
with open(persons_names_file_path,"r+") as f:
lineas = [linea.strip() for linea in f.readlines()]
with open(persons_identifier_img_file_path,"r+") as g:
lineas_Association = [lineaA.strip() for lineaA in g.readlines()]
print (person_name)
#print (persons_identifier_img)
print (lineas)
print (lineas_Association)
print("B")
#Si no se encuentra palabra en el archivo txt
if (person_name not in lineas):
print("A1")
f.write(f"{person_name}\n") #Agrega la palabra al final del txt de palabras
num_linea = lineas.index(person_name)
persons_identifier_img = str(num_linea) + ".jpg"
#Si llegase a estar el nombre pero no la imagen (quizas la borraron)
#elif (person_name in lineas) and (persons_identifier_img not in lineas_Association):
elif (person_name in lineas):
print("A3")
redefinition = True #SI NO SE ENCUENTRA ENTRA LA REDEFINICION Y ESO ESTA MAL DEBERIA SER SI LA PALABRA SE ENCUENTRA Y LA ASOCIACION ES DIFERENTE
num_linea = lineas.index(person_name) #Obtiene el indice que indica la linea en donde se escribio la palabra en el txt
persons_identifier_img = str(num_linea) + ".jpg"
with open(persons_identifier_img_file_path,"r+") as f:
lineas = [linea.strip() for linea in f.readlines()]
if (redefinition == False):
if(both_coincidences == False):
f.write(f"{persons_identifier_img}\n") #Agrega la definicion al final del txt de definiciones
answer_num = random.randint(1, 2)
if answer_num == 1:
#text = "Ohh vaya, no sabía que para jugar a " + str(word) + " necesitaría " + str(association)
print("No te conocia")
elif answer_num == 2:
#text = "Eso suena divertido, la verdad no sabía como jugar a " + str(word) + ", pero ahora se que para jugarle debería " + str(association)
print("No te conocia")
elif(both_coincidences == True):
#text = "Ohh eso ya lo sabía, si quieres podemos jugar a " + str(word)
print("Ya te conocia")
both_coincidences = False
if (redefinition == True):
f.truncate(0)
f.seek(0)
lineas[num_linea] = f"{persons_identifier_img}\n" #Agrega la definicion sobreescribiendo la definicion que ya estaba en la linea coincidente con la del txt de palabras
f.writelines("\n".join(lineas))
answer_num = random.randint(1, 2)
if answer_num == 1:
#text = "Ohh vaya, la verdad no sabía que para jugar a " + str(word) + ", además de lo que yo ya conocía, también se necesita eso que me estas diciendo acerca de que " + str(association) + ", por lo que veo creo que eso que me dices parece tener sentido"
print("Identifique una nueva imagen con tu personal, osea asociada a tu nombre")
elif answer_num == 2:
#text = "Si lo que me dices es correcto y te estoy entendiendo bien, para jugar a " + str(word) + " se necesita " + str(association) + ", jaja la verdad es que yo tenía otra información acerca de eso, pero lo que me dices creo que tiene sentido"
print("Identifique una nueva imagen con tu personal, osea asociada a tu nombre")
redefinition = False
return text
#except:
else:
print("V")
return text
input_text_str = input("ingrese: ")
text = ""
name_and_img_identificator(input_text_str, text)
File "do_you_talk_with.py", line 124, in name_and_img_identificator
lineas[num_linea] = f"{persons_identifier_img}\n"
IndexError: list assignment index out of range
Solution
num_linea
is the line number containing a person's name in the persons_names_file_path
file.
But then you try to access that same line number in the persons_identifier_img_file_path
file.
The second file has fewer lines, and so the index is out of range.
Answered By - John Gordon Answer Checked By - Marilyn (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.