Q:

I need to import a txt file as a list for a specific function

I am trying to save and load calibration data for my sensor. I was able to read the calibration data and save it to a text file. When looking at the text file there are 22 numbers separated by comas all in a set of square brackets. 
I need to import that text file in a way the set_calibration function will read it. 
Here is the set calibration function.
def set_calibration(self, data):
    """Set the sensor's calibration data using a list of 22 bytes that
    represent the sensor offsets and calibration data.  This data should be
    a value that was previously retrieved with get_calibration (and then
    perhaps persisted to disk or other location until needed again).
    """
    # Check that 22 bytes were passed in with calibration data.
    if data is None or len(data) != 22:
        raise ValueError('Expected a list of 22 bytes for calibration data.')
    # Switch to configuration mode, as mentioned in section 3.10.4 of datasheet.
    self._config_mode()
    # Set the 22 bytes of calibration data.
    self._write_bytes(ACCEL_OFFSET_X_LSB_ADDR, data)
    # Go back to normal operation mode.
    self._operation_mode()

Problem is, this is my first time using python and I have very very little coding experience. I tried this:
calfile=open("calibrate.txt","r")
caldata=calfile.readlines()
calfile.close()

After that caldata will print as "['[242, 255, 0, 0, 27, 0, 65, 2, 139, 3, 174, 255, 255, 255, 255, 255, 0, 0, 232, 3, 102, 3]']", but the set_calibration function returns the "expected a list of 22 bytes for calibration data." error. when run as
bno.set_calibration(caldata)

I'm not sure how much this will help, but the sensor is an Adafruit BNO055 and I am running it using a Raspberry Pi. I have a strong feeling I am misusing and/or abusing the read function, but, like i said, i am very new to this.

A:

calfile.readlines() isn't what you need to use here. That would be for if your numbers were all on different lines of the file, with no commas or square brackets, like this:
23
51
32

Because your numbers are already in a list-style format (ie. with commas and square brackets), we need to convert this into a real list rather than a string representation of one. To do this, replace your code between calfile=open... and calfile.close() with this:
caldata=[int(x.strip()) for x in calfile.read()[1:-1].split(",")]

This is a list comprehension that will convert the contents of your file into an actual Python list.

