如何将传感器输出转换为 csv 文件中的单个整数

发布于 2025-01-17 02:11:44 字数 1471 浏览 4 评论 0原文

我有一个 6 DoF 传感器,可将各种数据输出到文本文件中。目前文本输出看起来像

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

我希望它输出

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

在于 mpu.acceleration 如何输出到文件中

我的尝试如下:

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)

问题 一种将加速度的输出分解为不带括号和引号的单个整数的方法?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帅冕 2025-01-24 02:11:44

您可以将最终字符串转换为所需的格式。即,您可以取出括号内的值,然后将其附加到从双引号开始的字符串中。请参阅下面的代码。

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)

输出:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文