1

I have a 6 DoF sensor that outputs various data into a text file. Currently the text output looks like

14:46:25,22.835882352941177,"(-0.020917304775809907, 0.041168453348568536, -0.03810413481453269)"

And I would like it to output

14:46:25,22.835882352941177, -0.020917304775809907, 0.041168453348568536, -0.03810413481453269

The problem lies in how mpu.acceleration outputs into the file

My attempt so far is as follows:

def timec():
    now = datetime.now().time()
    current_time = now.strftime("%H:%M:%S")
    return current_time

def MPU():
    i2c = board.I2C()  # uses board.SCL and board.SDA
    mpu = adafruit_mpu6050.MPU6050(i2c)

    while True:
        print("Acceleration: X:%.2f, Y: %.2f, Z: %.2f m/s^2" % (mpu.acceleration))
        print("Gyro X:%.2f, Y: %.2f, Z: %.2f rad/s" % (mpu.gyro))
        print("Temperature: %.2f C" % mpu.temperature)
        print("")
        time.sleep(1)
        temp = mpu.temperature
        gy = mpu.gyro
        accel = mpu.acceleration
    
        return temp, gy, accel

if __name__ == '__main__':

    with open('Text FILE72', 'a', newline='') as f:
        write = csv.writer(f)
        while True:
            ctime = timec()
            temp, gy, accel = MPU()
            alt = BMP390()
            # something something = BNO055()
            # something something = solarcircuit1()
            Rows = (ctime, temp, gy) # add other variables such as bno055 and circuit
            write.writerow(Rows)
            f.flush()
            time.sleep(1)

Is there a way to dissect the output of acceleration into individual integers without parenthesis and quotes?

colem
  • 15
  • 4
  • we can't run this code - which variable gives `"(...)"`? maybe you should add every value from this variable separatelly - if it `gy` then `(ctime, temp, gy[0], gy[1], gy[2])` or using `*` unpack list/tuple `(ctime, temp, *gy)` – furas Mar 26 '22 at 23:54

1 Answers1

0

You can convert the final string to the required format. i.e, you could take out the values within parenthesis, and then append it to the string starting from double quotes. See below code.

line = '14:46:25,22.835882352941177,"(-0.020917304775809907, 0.041168453348568536, -0.03810413481453269)"'
print('actual line        : ',line)
within_paranthesis = line[line.find("(")+1:line.find(")")]


line_without_paranthesis = line[0:line.find('"')] + within_paranthesis
print('without paranthesis: ', line_without_paranthesis)

Output:

actual line        :  14:46:25,22.835882352941177,"(-0.020917304775809907, 0.041168453348568536, -0.03810413481453269)"
without paranthesis:  14:46:25,22.835882352941177,-0.020917304775809907, 0.041168453348568536, -0.03810413481453269
Manjunath K Mayya
  • 1,078
  • 1
  • 11
  • 20