pythonhelper

How to turn content from a text file into an integer variable


I am fairly new to programming and am trying to create a script. What I am trying to do is I have data written to a text file in this format x, y\n. I then want to get that same content from the text file and assign it to a variable as an integer so I could use for example as graph cords. I.E turtle.goto(variable). Simplified: I have data that is 1400, 800. I write that to a text file with a newline. I then want to read that same line and have those two numbers as integers for a variable. turtle.goto(variable) = turtle.goto(1400, 800).

config.txt contains "1400,1800\n"
file = open('config.txt')
content = file.readlines()
commentz = content[0].strip('\n')   
pyautogui.click(commentz)

Solution

  • Read the lines of config.txt file.
    Remove the whitespace and \n characters from every line with strip().
    Split the numbers into x and y variables with split(',').
    Convert x and y to int values with int(x) and int(y).

    with open('config.txt', 'r') as f:
        lines = f.readlines()
        for line in lines:
            x, y = line.strip().split(',')
            pyautogui.click(int(x), int(y))