如何在Python中同时写入两个CSV?

联系我们背后的科学

我有两个无线电,sdr和sdr2,接收数据,我想将该日期(是复数)保存在CSV文件中。我需要同时从两个无线电中获取数据,每个无线电都运行5次扫描,因此我在代码主要部分中所做的是:

#we save the sdr and sdr2 in the same array
radios = [ sdr, sdr2]
pool = ThreadPool(4)
#create an object of class Scan
s=Scan()
#pool.map(function, array)
pool.map(s.scan, radios)
pool.close() 
pool.join()

然后,扫描功能为:

class Scan: 
    def scan(self, object):   
      for i in range(0,1):
        #Read iq data
        samples = object.read_samples(256*1024)
        #print(samples)

       #get the maximum amplitude in frequency to save IQ samples if it's greater
       #than -1 dB
        sp = np.fft.fft(samples)
        ps_real=sp.real
        ps_imag=sp.imag
        sq=np.power(ps_real,2)+np.power(ps_imag,2)
        sqrt=np.sqrt(sq)
        psd=sqrt/1024
        value=[ps_real,ps_imag]
        max=np.max(psd)
        log=10*math.log10(max)
        print(value)
        current_time = time.strftime("%m.%d.%y-%H%M.csv", time.localtime())
        if log > -1:
            #save the IQ data in csv
            with open('%s' % current_time, 'w',newline='') as f:
                writer = csv.writer(f, delimiter=',')
                writer.writerows(zip(ps_real,ps_imag))

但这是从一个收音机(我认为是唯一一个)的最后一次迭代中获得阵列(真实,imag对)并将其保存在唯一的CSV中...我想拥有2个不同的CSV,因此,我将时间戳记为CSV名称,并且还需要记录任何迭代中的数据。关于如何解决这个问题的任何想法?谢谢!

网波

您将在同一天,同一小时和同一分钟打开输出文件,因此您要在两个作业中写入同一文件,只需使该函数使用id并将其作为参数传递即可:

class Scan: 
    def scan(self, id, object):
        ...
        current_time = time.strftime("%m.%d.%y-%H%M", time.localtime())
        if log > -1:
            #save the IQ data in csv
            with open('{}_{}.csv' .format(current_time, id), 'w',newline='') as f:
                ...

然后enumerate在将其映射到线程池中时使用包装器将ID从中解压缩到无线电中:

#we save the sdr and sdr2 in the same array
radios = [ sdr, sdr2]
pool = ThreadPool(4)
#create an object of class Scan
s=Scan()

def scan(args_tuple):
    global s
    id, code = args_tuple
    return s.scan(id, code)

pool.map(scan, enumerate(radios))
pool.close() 
pool.join()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在android studio中的两个活动中同时动态创建TextView?

同时创建和写入两个CSV golang

如何在Android Studio中同时显示两个或多个logcat过滤器?

如何在C#中同时迭代两个非平凡的类对象的属性,类似于Python的zip方法

如何在JQuery中同时制作两个动画?

Postgres:从csv同时在两个表中插入值

如何在MacBook上同时运行两个python程序?

如何在python中合并两个csv文件

如何在Groovy(Jenkins)中从两个文件写入一个文件?

如何在一个函数中同时运行两个enumerateNodes

如何在函数之间同时交换两个变量?

如何在Spark构造的流中将两个流df写入MySQL的两个不同表中?

如何在python中的for循环中同时检查两个条件?

如何在两个不同的线程中同时执行PyObject_CallObject()?

如何在uipickerview中同时旋转两个组件

当两个日期不同时,如何在javascript中获取两个日期之间的差异

如何在R中同时grep两个词

如何在每次迭代中错开动画并同时触发两个动画

如何在PHP的一个函数中同时使用两个变量

如何比较两个csv文件并使用python在新文件中写入1或0

如何在bash中同时重复两个for循环?

如何在notepad++中同时搜索两个项目

如何在 Laravel 中同时保存两个模型?

如何在 python Flask 中对两个或多个 CSV 文件进行验证

如何在reactjs中同时绑定两个方法

如何在spark中同时拆分数据帧中的两个映射列

如何在 Django 中同时获取两个模型

如何在 Python 中比较两个 CSV 文件?

如何在从python中的动态输入中删除重复项的同时组合两个列表