Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5

Evaluate a string of a list of values as a variable in Python

#1
I want to take the entrys from a txt file but right know it shows me this error , has everyone faced this kind of problem before?

   

Code:
import os 
file_path = r'C:\Users\giahn\OneDrive\Έγγραφα\Σχολή\ΔΙΠΛΩΜΑ\main\ready to use\entry1.txt'
if os.path.exists(file_path):
    with open(file_path, 'r') as file:
         content = file.readline().strip()  
       
         start_target = int(content)     #this lane has the problem
       
else:
    print("file not found")
#2
You can convert a string to an int unless it represent an integer value.

However, if you want to evaluate a string that represents a list of values represented the same way you would write it in Python, you can return the value by using the eval function in Python.

Example:
Code:
xyz = eval(content)
#3
ok but also this doesn't work [xyz = eval(content)] the values that i want to copy is [1000,0,1800] and the reason is because they're the coordinates for the robot .
#4
I recommend you to use a debugger such as VSCode or VSCodium and add a breakpoint before an exception is raised to inspect the value of each variable.

Can you provide the lines of code that raise an exception?
  




Users browsing this thread:
1 Guest(s)