Skip to content
Advertisement

I’m getting an error when comparing in text file with python

X,Y Matrix list

In this matrix, like adding items such as 1×3,3×3,5×5 to the map, check each item according to the item id and if it is 1×3 and start from the starting coordinate, for example X:5 Y1, if the item is horizontal and it is , then push it to the y-axis once and to the x-axis 3 times. I would like to add the id I assigned and add the ones with the same id separately, for example, there are two items with item id 82, but one is number 1, the other is number 2 and 0. I want to compare the shortest path from the non-item squares by scanning them one by one. but I’m having trouble reading the txt files with the dimensions of the items. I was reading it directly as .read() before and I noticed that the items were measuring incorrectly.

The code where I check the item sizes

def propsize(propid):

    path1x1 = os.getcwd() + "/settings/PropSize/1x1.txt"
    path1x3 = os.getcwd() + "/settings/PropSize/1x3.txt"
    path1x5 = os.getcwd() + "/settings/PropSize/1x5.txt"
    path1x10 = os.getcwd() + "/settings/PropSize/1x10.txt"
    path1x19 = os.getcwd() + "/settings/PropSize/1x19.txt"
    path3x3 = os.getcwd() + "/settings/PropSize/3x3.txt"
    path3x5 = os.getcwd() + "/settings/PropSize/3x5.txt"
    path4x7 = os.getcwd() + "/settings/PropSize/4x7.txt"
    path4x8 = os.getcwd() + "/settings/PropSize/4x8.txt"
    path5x5 = os.getcwd() + "/settings/PropSize/5x5.txt"
    path6x6 = os.getcwd() + "/settings/PropSize/6x6.txt"
    path7x7 = os.getcwd() + "/settings/PropSize/7x7.txt"
    path8x8 = os.getcwd() + "/settings/PropSize/8x8.txt"
    path9x9 = os.getcwd() + "/settings/PropSize/9x9.txt"
    path10x10 = os.getcwd() + "/settings/PropSize/10x10.txt"
    path20x20 = os.getcwd() + "/settings/PropSize/20x20.txt"

    p1x1 = open(path1x1,"r").readlines()
    p1x3 = open(path1x3,"r").readlines()
    p1x5 = open(path1x5,"r").readlines()
    p1x10 = open(path1x10,"r").readlines()
    p1x19 = open(path1x19,"r").readlines()
    p3x3 = open(path3x3,"r").readlines()
    p3x5 = open(path3x5,"r").readlines()
    p4x7 = open(path4x7,"r").readlines()
    p4x8 = open(path4x8,"r").readlines()
    p5x5 = open(path5x5,"r").readlines()
    p6x6 = open(path6x6,"r").readlines()
    p7x7 = open(path7x7,"r").readlines()
    p8x8 = open(path8x8,"r").readlines()
    p9x9 = open(path9x9,"r").readlines()
    p10x10 = open(path10x10,"r").readlines()
    p20x20 =  open(path20x20,"r").readlines()

    for p in p1x1:
            if propid == p:
                    print("1x1")
                    return "1:1"
    for p in p1x3:       
            if propid == p:
                    print("1x3")
                    return "1:3"
    for p in p1x5:        
            if propid == p:
                    print("1x5")
                    return "1:5"
    for p in p1x10:
            if propid == p:
                    print("1x10")
                    return "1:10"
    for p in p1x19:
            if propid == p:
                    print("1x19")
                    return "1:19"
    for p in p3x3:
            if propid == p:
                    print("3x3")
                    return "3:3"   
    for p in p3x5:
            if propid == p:
                    print("3x5")
                    return "3:5" 
    for p in p4x7:
            if propid == p:
                    print("4x7")
                    return "4:7"
    for p in p4x8:        
            if propid == p:
                    print("4x8")
                    return "4:8"
    for p in p5x5:
            if propid == p:
                    print("5x5")
                    return "5:5"
    for p in p6x6:
            if propid == p:
                    print("6x6")
                    return "6:6"
    for p in p7x7:
            if propid == p:
                    print("7x7")
                    return "7:7"
    for p in p8x8:
            if propid == p:
                    print("8x8")
                    return "8:8"
    for p in p9x9:
            if propid == p:
                    print("9x9")
                    return "9:9"
    for p in p10x10:        
            if propid == p:
                    print("10x10")
                    return "10:10"
    for p in p20x20:
            if propid == p:
                    print("20x20")
                    return "20:20"
    return "!:!"

The code where I add the items to the matrix

def fill_flats(pid, corx,cory,sizex,sizey,rot):


if rot == "h":
    yt = 0
    
    while yt < sizey:
        xt = 0
        while xt < sizex:
            global Y1, Y2, Y3, Y4, Y5 , Y6, Y7, Y8, Y9 , Y10, Y11, Y12 , Y13, Y14, Y15, Y16, Y17 , Y18, Y19, Y20
            exec ("Y{}[{}]={}".format(str(cory+xt),corx+yt,pid))

            xt +=1
        yt+=1    
if rot == "v":
    xt = 0
    
    while xt < sizex:
        yt = 0
        while yt < sizey:
            global Y1, Y2, Y3, Y4, Y5 , Y6, Y7, Y8, Y9 , Y10, Y11, Y12 , Y13, Y14, Y15, Y16, Y17 , Y18, Y19, Y20
            exec ("Y{}[{}]={}".format(str(cory+yt),corx+xt,pid))

            yt +=1
        xt+=1 

And this is the error I get

 /Tructer/test.py", line 188, in importmap
  fill_flats(propanalyzed[o]["pnum"],int(propanalyzed[o] 
   ["cordinatesX"]),int(propanalyzed[o]["cordinatesY"]),int(propanalyzed[o] 
    ["X"]),int(propanalyzed[o]["Y"]),str(propanalyzed[o]["rotation"]))       
     ValueError: invalid literal for int() with base 10: '!'

size function returning: “!:!” where am i doing wrong:(

My full test.py code

from asyncio import queues
from http import cookies
import os
from posixpath import split
from textwrap import fill
import time
from typing_extensions import Self
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from pyfiglet import Figlet
from rich.progress import track
xcursor =1
ycursor =1

title = Figlet(font='slant')
print(title.renderText('TRUCTER'))

Y1 =   [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
Y2 =   [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
Y3 =   [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
Y4  =  [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
Y5  =  [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
Y6  =  [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
Y7  =  [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
Y8  =  [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
Y9  =  [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
Y10 =  [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
Y11 =  [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
Y12 =  [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
Y13 =  [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
Y14 =  [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
Y15 =  [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
Y16 =  [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
Y17 =  [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
Y18 =  [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
Y19 =  [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
Y20 =  [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]


def checklogged():
       
       
         
        for cookie in cookies:
                
            if cookie['name'] == "ltc_user":
                    print(cookie)
            
def propsize(propid):
        path1x1 = os.getcwd() + "/settings/PropSize/1x1.txt"
        path1x3 = os.getcwd() + "/settings/PropSize/1x3.txt"
        path1x5 = os.getcwd() + "/settings/PropSize/1x5.txt"
        path1x10 = os.getcwd() + "/settings/PropSize/1x10.txt"
        path1x19 = os.getcwd() + "/settings/PropSize/1x19.txt"
        path3x3 = os.getcwd() + "/settings/PropSize/3x3.txt"
        path3x5 = os.getcwd() + "/settings/PropSize/3x5.txt"
        path4x7 = os.getcwd() + "/settings/PropSize/4x7.txt"
        path4x8 = os.getcwd() + "/settings/PropSize/4x8.txt"
        path5x5 = os.getcwd() + "/settings/PropSize/5x5.txt"
        path6x6 = os.getcwd() + "/settings/PropSize/6x6.txt"
        path7x7 = os.getcwd() + "/settings/PropSize/7x7.txt"
        path8x8 = os.getcwd() + "/settings/PropSize/8x8.txt"
        path9x9 = os.getcwd() + "/settings/PropSize/9x9.txt"
        path10x10 = os.getcwd() + "/settings/PropSize/10x10.txt"
        path20x20 = os.getcwd() + "/settings/PropSize/20x20.txt"

        p1x1 = open(path1x1,"r").readlines()
        p1x3 = open(path1x3,"r").readlines()
        p1x5 = open(path1x5,"r").readlines()
        p1x10 = open(path1x10,"r").readlines()
        p1x19 = open(path1x19,"r").readlines()
        p3x3 = open(path3x3,"r").readlines()
        p3x5 = open(path3x5,"r").readlines()
        p4x7 = open(path4x7,"r").readlines()
        p4x8 = open(path4x8,"r").readlines()
        p5x5 = open(path5x5,"r").readlines()
        p6x6 = open(path6x6,"r").readlines()
        p7x7 = open(path7x7,"r").readlines()
        p8x8 = open(path8x8,"r").readlines()
        p9x9 = open(path9x9,"r").readlines()
        p10x10 = open(path10x10,"r").readlines()
        p20x20 =  open(path20x20,"r").readlines()

        for p in p1x1:
                if propid == p:
                        print("1x1")
                        return "1:1"
        for p in p1x3:       
                if propid == p:
                        print("1x3")
                        return "1:3"
        for p in p1x5:        
                if propid == p:
                        print("1x5")
                        return "1:5"
        for p in p1x10:
                if propid == p:
                        print("1x10")
                        return "1:10"
        for p in p1x19:
                if propid == p:
                        print("1x19")
                        return "1:19"
        for p in p3x3:
                if propid == p:
                        print("3x3")
                        return "3:3"   
        for p in p3x5:
                if propid == p:
                        print("3x5")
                        return "3:5" 
        for p in p4x7:
                if propid == p:
                        print("4x7")
                        return "4:7"
        for p in p4x8:        
                if propid == p:
                        print("4x8")
                        return "4:8"
        for p in p5x5:
                if propid == p:
                        print("5x5")
                        return "5:5"
        for p in p6x6:
                if propid == p:
                        print("6x6")
                        return "6:6"
        for p in p7x7:
                if propid == p:
                        print("7x7")
                        return "7:7"
        for p in p8x8:
                if propid == p:
                        print("8x8")
                        return "8:8"
        for p in p9x9:
                if propid == p:
                        print("9x9")
                        return "9:9"
        for p in p10x10:        
                if propid == p:
                        print("10x10")
                        return "10:10"
        for p in p20x20:
                if propid == p:
                        print("20x20")
                        return "20:20"
        return "!:!"
def importmap():
        designfile = "/settings/design.txt"
        designParsedfile = "/settings/design_parsed.txt"
        path_d = os.getcwd()+designfile
        path_dp = os.getcwd()+designParsedfile
        propnumber = 1
        print('Design Analyzing...')
        with open(path_d) as f:
                propanalyzed = {}
                props = f.readline().replace(" ","").split(";")
                props.__delitem__(len(props)-1)
                pnumber = 1
                for prop in props:
                   
                        p = prop.split(",")
                        
                        ps = propsize(p[0].replace(" ",""))
                        p_size = ps.split(":")
                
                        propanalyzed[propnumber] = {
                                "propid"      : p[0],
                                "X"           : p_size[0],
                                "Y"           : p_size[1],
                                "cordinatesX" : int(p[1])+1,
                                "cordinatesY" : int(p[2])+1,
                                "rotation"    : p[3],
                                "pnum"        : pnumber
                        }
                        propnumber = propnumber+1
                        pnumber +=1
                pnumber = 1
                propnumber=1
        pathanalyzed = os.getcwd() + "/settings/PropSize/propanalyzed.txt"
        open(pathanalyzed, "a").write(str(propanalyzed) + "n") 
        print('Map Visualation in Progress...')
        
        
        for o in propanalyzed:
                fill_flats(propanalyzed[o]["pnum"],int(propanalyzed[o]["cordinatesX"]),int(propanalyzed[o]["cordinatesY"]),int(propanalyzed[o]["X"]),int(propanalyzed[o]["Y"]),str(propanalyzed[o]["rotation"]))       
        

def move_x(a,b):
        global xcursor
        if a == "+":
                xcursor = xcursor + b
        elif a=="-":
                xcursor = xcursor - b
def move_y(a,b):
        global ycursor
        if a == "+":
                ycursor = ycursor + b
        elif a=="-":
                ycursor = ycursor - b
              
def fill_flats(pid, corx,cory,sizex,sizey,rot):
    

    if rot == "h":
        yt = 0
        
        while yt < sizey:
            xt = 0
            while xt < sizex:
                global Y1, Y2, Y3, Y4, Y5 , Y6, Y7, Y8, Y9 , Y10, Y11, Y12 , Y13, Y14, Y15, Y16, Y17 , Y18, Y19, Y20
                exec ("Y{}[{}]={}".format(str(cory+xt),corx+yt,pid))

                xt +=1
            yt+=1    
    if rot == "v":
        xt = 0
        
        while xt < sizex:
            yt = 0
            while yt < sizey:
                global Y1, Y2, Y3, Y4, Y5 , Y6, Y7, Y8, Y9 , Y10, Y11, Y12 , Y13, Y14, Y15, Y16, Y17 , Y18, Y19, Y20
                exec ("Y{}[{}]={}".format(str(cory+yt),corx+xt,pid))

                yt +=1
            xt+=1 
            



importmap()
pr = 1
mapppt = os.getcwd() + "/settings/mapp.txt"

while pr < 20:
        
        open(mapppt, 'a').write(str(str(Y1[pr])+"  | " + str(Y2[pr])+"  | " + str(Y3[pr])+"  | " + str(Y4[pr])+"  | " + str(Y5[pr])+"  | " + str(Y6[pr])+"  | " + str(Y7[pr])+"  | " + str(Y8[pr])+"  | " + str(Y9[pr])+"  | " + str(Y10[pr])+"  | " + str(Y11[pr])+"  | " + str(Y12[pr])+"  | " + str(Y13[pr])+"  | " + str(Y14[pr])+"  | " + str(Y15[pr])+"  | " + str(Y16[pr])+"  | " + str(Y17[pr])+"  | " + str(Y18[pr])+"  | " + str(Y19[pr])+"  | " + str(Y20[pr])+"  | ")+ 'n')                   
        pr +=1

And example propsize file

5
6
21
22
23
24
25
26
27
28
80
81
82
84

And example item before processing(82 is item number , 0 is x cordinate and 1 is y cordinate and “v” is rotation type

82,0,1,v; 

Advertisement

Answer

You are getting a ValueError, but the problem with your code isn’t this.


exec ("Y{}[{}]={}".format(str(cory+xt),corx+yt,pid))

This is the problem. Creating Y1 Y2 Y3Y20 is a very bad idea, try something like this instead:

Y = []
for _ in range(20):
    Y.append([0, 0, 0, 0, 0, 0, 0, 0, 0])

then you can access it this way:

for i in range(20):
    for j in range(10): # Instead of 10 put the lenght of your inner lists
        Y[i][j]
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement