动态保存到CSV使用数组

发布于 2025-02-11 13:17:52 字数 2717 浏览 0 评论 0原文

我正在使用CSV模块将数组保存到CSV,但是我想使其更具动态性。抱歉,如果这已经在这里,我尝试搜索,但无济于事。 这是我当前的代码,它运行良好。...

filename = 'zeroTime_.csv'        #save to csv
f = open(filename, "w")
f.write("{},{},{},{}\n".format("R[0]", "F[0]","R[1]", "F[1]"))
for x in zip(R[0], F[0],R[1], F[1]):
    f.write("{},{},{},{}\n".format(x[0], x[1], x[2], x[3]))
f.close()

但是我想制作,以便如果Array R有超过2列,它仍然可以正常工作 我尝试创建“ r [0]”,“ f [0]”,“ r [1]”,“ f [1]”作为字符串,但不喜欢它,我的代码为每个通道 r r f 由numch

for x in range(NumCh):
    title = title +'"R['+str(x)+']",'+'"F['+str(x)+']",'
    bracket = bracket + '{},{},'
title = title[:-1]
bracket = bracket[:-1]
bracket = bracket + '\n"'

filename = 'zeroTime_.csv'        #save to csv
f = open(filename, "w")
f.write(bracket.format(title))
for x in zip(R[0], F[0],R[1], F[1]):
    f.write(bracket.format(x[0], x[1], x[2], x[3]))
f.close()

给我错误:(

Traceback (most recent call last):
  File "base.py", line 8, in <module>
    b = pd.proc(NumCh, a)
  File "C:\Users\jtpkalaverick\Documents\Python\module\time_proc.py", line 24, in proc
    f.write(bracket.format(title))
IndexError: Replacement index 1 out of range for positional args tuple

我正在模块中运行它以pd。)

编辑(30.06.22)

我有2个模块tick_proc,该模块会产生一些任意的数组和time_proc,并且在数组上可以做一些基本的数学。 Numch和样本传递到模块中,只是INTS

主代码:

import tick_proc as tp
import time_proc as pd
NumCh = 2
samples = 10

a = tp.collect_data(NumCh, samples)
b = pd.proc(NumCh, a)
print('b', b)

tick Proc:

print('Importing module "TickProc"')
R = []
F = []
def collect_data(NumCh, samples):
    for x in range(NumCh):
        R.append([])
        F.append([])

    for x in range(NumCh):
        for y in range(samples):
            R[x].append(y*x)
            F[x].append(y*x -1)

    return F, R

time_proc: time_proc:

import csv
def proc(NumCh, a):
    R = a[0]
    F = a[1]
    T = []
    bracket = '"'
    title = ''
    vars = ''

    for x in range(NumCh):
        T.append([])
        title = title +'"R['+str(x)+']",'+'"F['+str(x)+']",'
        bracket = bracket + '{},{},'
    title = title[:-1]
    bracket = bracket[:-1]
    bracket = bracket + '\n"'
    print(bracket)
    print(title)
    for i in range(NumCh):
        for j in range(len(R[i])):
            T[i].append(R[i][j]-F[i][j])

    filename = 'zeroTime_.csv'        #save to csv
    f = open(filename, "w")
    f.write(bracket.format(title))
    for x in zip(R[0], F[0],R[1], F[1]):
        f.write(bracket.format(x[0], x[1], x[2], x[3]))
    f.close()
    return T

I'm using the csv module to save arrays to csv, however I want to make it abit more dynamic. Apologies if this is already up here, I tried searching but to no avail.
this is my current code, it works well....

filename = 'zeroTime_.csv'        #save to csv
f = open(filename, "w")
f.write("{},{},{},{}\n".format("R[0]", "F[0]","R[1]", "F[1]"))
for x in zip(R[0], F[0],R[1], F[1]):
    f.write("{},{},{},{}\n".format(x[0], x[1], x[2], x[3]))
f.close()

However I want to make it so that if array R has more than 2 columns in, it will still work
I tried creating the "R[0]", "F[0]","R[1]", "F[1]" as a string and just using that but it doesnt like it, my code creates a R and F for each channel denoted by NumCh:

for x in range(NumCh):
    title = title +'"R['+str(x)+']",'+'"F['+str(x)+']",'
    bracket = bracket + '{},{},'
title = title[:-1]
bracket = bracket[:-1]
bracket = bracket + '\n"'

filename = 'zeroTime_.csv'        #save to csv
f = open(filename, "w")
f.write(bracket.format(title))
for x in zip(R[0], F[0],R[1], F[1]):
    f.write(bracket.format(x[0], x[1], x[2], x[3]))
f.close()

Gives me the error:

Traceback (most recent call last):
  File "base.py", line 8, in <module>
    b = pd.proc(NumCh, a)
  File "C:\Users\jtpkalaverick\Documents\Python\module\time_proc.py", line 24, in proc
    f.write(bracket.format(title))
IndexError: Replacement index 1 out of range for positional args tuple

(I'm running this within a module addressed as pd.)

edit (30.06.22)

i have 2 modules tick_proc that produces some arbitrary arrays and time_proc that does some basic maths on the arrays. NumCh and samples are passed into the modules and are just ints

main code:

import tick_proc as tp
import time_proc as pd
NumCh = 2
samples = 10

a = tp.collect_data(NumCh, samples)
b = pd.proc(NumCh, a)
print('b', b)

tick proc:

print('Importing module "TickProc"')
R = []
F = []
def collect_data(NumCh, samples):
    for x in range(NumCh):
        R.append([])
        F.append([])

    for x in range(NumCh):
        for y in range(samples):
            R[x].append(y*x)
            F[x].append(y*x -1)

    return F, R

time_proc:

import csv
def proc(NumCh, a):
    R = a[0]
    F = a[1]
    T = []
    bracket = '"'
    title = ''
    vars = ''

    for x in range(NumCh):
        T.append([])
        title = title +'"R['+str(x)+']",'+'"F['+str(x)+']",'
        bracket = bracket + '{},{},'
    title = title[:-1]
    bracket = bracket[:-1]
    bracket = bracket + '\n"'
    print(bracket)
    print(title)
    for i in range(NumCh):
        for j in range(len(R[i])):
            T[i].append(R[i][j]-F[i][j])

    filename = 'zeroTime_.csv'        #save to csv
    f = open(filename, "w")
    f.write(bracket.format(title))
    for x in zip(R[0], F[0],R[1], F[1]):
        f.write(bracket.format(x[0], x[1], x[2], x[3]))
    f.close()
    return T

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文