csv 在for循环中根据i粘贴变量名的结尾

iyzzxitl  于 2023-09-28  发布在  其他
关注(0)|答案(1)|浏览(73)

我试图将每个变量save_i(直到长度(计数器))保存为csv文件中的一行。每个保存变量都是在while循环中定义的,所以我有save_1:save_i,其中i= length(counter)。代码的一部分如下:

# Packages
import msvcrt
import time
import sounddevice as sd
import soundfile as sf
import csv

# Settings
PLAY_TIME = 5  # in seconds

# Sound
sound_file_1 = 'C:/Users/am650/Desktop/Audio/aaa_1.mp3'
sound_file_2 = 'C:/Users/am650/Desktop/Audio/aaa_2.mp3'
blast_file_1, fs1 = sf.read(sound_file_1, dtype='float32')
blast_file_2, fs2 = sf.read(sound_file_2, dtype='float32')

# Set Up
time_start = time.perf_counter()
time_stop = time.perf_counter()
time_elapsed = time_stop - time_start
counter = 0

# Basic Logic
while time_elapsed < PLAY_TIME:
    blast = msvcrt.getch()
    if blast == b'1':
        blast = 1
        time_in = time.perf_counter()
        time_to_button_press = time_in - time_start
        sd.play(blast_file_1, device=4)
        status = sd.wait()
        time_in = time.perf_counter()
        time_to_blast_end = time_in - time_start
        counter += 1
        save = "save_"+str(counter)
        globals()[save] = (counter, blast, time_to_button_press, time_to_blast_end)
        print(eval("save_"+str(counter)))
        if time_to_blast_end > PLAY_TIME:
            break
    elif blast == b'2':
        blast = 2
        time_in = time.perf_counter()
        time_to_button_press = time_in - time_start
        sd.play(blast_file_2, device=4)
        status = sd.wait()
        time_in = time.perf_counter()
        time_to_blast_end = time_in - time_start
        counter += 1
        save = "save_"+str(counter)
        globals()[save] = (counter, blast, time_to_button_press, time_to_blast_end)
        print(eval("save_"+str(counter)))
        if time_to_blast_end > PLAY_TIME:
            break
    else:
        print("Invalid Entry")

with open('C:/Users/am650/Desktop/testdata.csv', 'w', newline='') as csvfile:
    datawriter = csv.writer(csvfile, delimiter=' ')
    for i in range(counter):
        datawriter.writerow(eval("save_"+str(i)))

我已经尝试了最后一行代码(datawriter.writerow(eval("save_"+str(i))))的几个变体,但没有一个能像预期的那样工作。上面返回一个空的excel工作表。datawriter.writerow("save_"+str(i)返回“save_1”、“save_2”等。直到长度(计数器),因为它们有自己的行,而不是保存在这些变量中的数据。datawriter.writerow("save_"+str(counter))给出了最后一轮重复i行的数据(这告诉我for循环工作正常);此外,这给了我最后一个保存变量,其中所有组件(counter,blast,time_to_button_press和time_to_blast_end)都在一列中,由空格分隔,而不是每个组件都在自己的列中。
我希望代码给予我一个Excel表,看起来像这样:
r1:1、4、0.5、1
r2:2、2、1.7、2.3
r3:3、5、4、4.5
其中每行有四列并包含来自save_1:save_i的数据
任何帮助都是巨大的:)请原谅我的代码的不雅,我几乎只在r中工作。我试着使用一堆已经回答过的类似问题,它们让我更接近我的目标,但不是所有的方式。

mrwjdhj3

mrwjdhj31#

我认为你正在做大量的工作来手动完成一组列表可能为你做的事情。与其创建一堆变量foo_1foo_2,....,foo_n,不如使用列表或字典来存储这些变量。
以下是我最初可能会如何处理这个问题:

# Packages
import csv
import msvcrt
import time

import sounddevice as sd
import soundfile as sf

# Settings
PLAY_TIME = 5  # in seconds

# Sounds
blast_files = {
    "1": sf.read('C:/Users/am650/Desktop/Audio/aaa_1.mp3', dtype='float32'),
    "q": sf.read('C:/Users/am650/Desktop/Audio/aaa_q.mp3', dtype='float32'),
}
save_files = []

counter = 0
time_start = time.perf_counter()
time_elapsed = time.perf_counter() - time_start

while time_elapsed < PLAY_TIME:
    blast_key = msvcrt.getch().decode()
    if not blast_key in blast_files:
        print("Invalid Entry")
        time_elapsed = time.perf_counter() - time_start
        continue

    counter += 1
 
    time_to_button_press = time.perf_counter() - time_start
    sd.play(blast_files[blast_key], device=4)
    status = sd.wait()
    time_to_blast_end = time.perf_counter() - time_start
 
    save_files.append([counter, blast_key, time_to_button_press, time_to_blast_end])
    time_elapsed = time.perf_counter() - time_start

with open('C:/Users/am650/Desktop/testdata.csv', 'w', newline='') as csvfile:
    datawriter = csv.writer(csvfile, delimiter=' ')
    for row in save_files:
        print(row)  ## just to demonstrate that row here should be an item stored it the list save_files
        datawriter.writerow(row)

我没有声音库,但这里有一个版本,为我工作。它接受1的输入,并将打印文件列表和构造csv。

# Packages
import csv
import msvcrt
import time

# Settings
PLAY_TIME = 5  # in seconds

# Sounds
blast_files = {
    "1": 'C:/Users/am650/Desktop/Audio/aaa_1.mp3',
    "q": 'C:/Users/am650/Desktop/Audio/aaa_q.mp3',
}
save_files = []

counter = 0
time_start = time.perf_counter()
time_elapsed = time.perf_counter() - time_start

while time_elapsed < PLAY_TIME:
    blast_key = msvcrt.getch().decode()

    if not blast_key in blast_files:
        print("Invalid Entry")
        time_elapsed = time.perf_counter() - time_start
        continue

    counter += 1
 
    time_to_button_press = time.perf_counter() - time_start
    print(blast_files[blast_key])
    time_to_blast_end = time.perf_counter() - time_start
 
    save_files.append([counter, blast_key, time_to_button_press, time_to_blast_end])
    time_elapsed = time.perf_counter() - time_start

with open('testdata.csv', 'w', newline='') as csvfile:
    datawriter = csv.writer(csvfile, delimiter=' ')
    for row in save_files:
        print(row)  ## just to demonstrate that row here should be an item stored it the list save_files
        datawriter.writerow(row)

相关问题