python 3 csv写入while循环

时间:2016-09-19 18:16:37

标签: python-3.x csv

我似乎无法将我的代码写入csv文件。

有人可以帮帮我吗?写入csv文件似乎从python 2.7 - 3改变。

def csv_writer(data, path):

    with open(path, "w", newline='') as csv_file:
        writer = csv.writer(csv_file)
        for line in data:
        writer.writerow(line)
if __name__ == '__main__':
    with MMA8452Q() as accelerometer:
        accelerometer.standby()
        accelerometer.set_g_range(G_RANGE)
        accelerometer.activate()
        time.sleep(INTERVAL)
 while True:
            raw = accelerometer.get_xyz(raw=True)
            g = accelerometer.get_xyz()
            ms = accelerometer.get_xyz_ms2()
            data = ('  {},  {},  {}, {}'.format(raw['x'], raw['y'], raw['z'], datetime.datetime.now()))
            csv_file.flush()
            path = "output.csv"
            csv_writer(data, path)
            time.sleep(INTERVAL)

0 个答案:

没有答案