如何将传感器输出转换为 csv 文件中的单个整数
我有一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将最终字符串转换为所需的格式。即,您可以取出括号内的值,然后将其附加到从双引号开始的字符串中。请参阅下面的代码。
输出:
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.
Output: