python语言中的csv文件问题

时间:2019-05-22 08:31:16

标签: python csv

假设我有一个文件/目录,其中存在许多.csv文件,并且我有一个python代码,它只能读取一个csv文件并执行某种算法并将输出存储在另一个csv文件中。现在我需要更新该python代码,以便我们可以检查文件/目录并将所有csv文件(存在于目录中)的输出存储在不同的csv文件中。

import pandas as pd
import statistics as st
import csv

data = pd.read_csv('1mb.csv')
x_or = list(range(len(data['Main Avg Power (mW)'])))
y_or = list(data['Main Avg Power (mW)'])

time=list(data['Time (s)'])
rt=5000
i=time[rt]
k=i
tlist=[]
for i in time:
    tlist.append(y_or[rt])
    rt+=1
    if i-k>4:
        break
idp=st.mean(tlist)
sidp=st.stdev(tlist)
newlist=[]
imax=max(tlist)
imin=min(min(tlist),idp-sidp)
while imax>=y_or[rt]>=imin-1:
         newlist.append(y_or[rt])
         rt+= 1
print(rt,"Mean idle power:",st.mean(newlist),"mW")
midp=st.mean(newlist)
with open('new_1pp.csv','w',newline='') as f:
    thewriter=csv.writer(f)

    thewriter.writerow(['Idle Power(mW)'])
    thewriter.writerow([midp])

这是我完成的代码。请根据问题中的要求对其进行更新。

1 个答案:

答案 0 :(得分:0)

在您的代码中,您可以使用glob列出目录中的所有CSV文件,然后一次将它们全部通过您拥有的任何算法进行读取,然后再次输出它们,例如

import glob
import os

# set the name of the directory you want to list the files in
csvdir = 'my_directory'

# get a list of all CSV files, assuming they have a '.csv' suffix
csvfiles = glob.glob(os.path.join(csvdir, '*.csv'))

# loop over all the files and run your algorithm
for csvfile in csvfiles:
    # read the csvfile using your current code

    # apply your algorithm

    # output a new file (e.g. with the same name as before, but with '_new' added
    newfile = csvfile.rstrip('.csv') + '_new.csv'

    # save to 'newfile' using yor current code

有帮助吗?

更新

从评论和更新的问题中,以下代码是否有帮助:

import pandas as pd
import statistics as st
import csv
import glob

# get list of CSV files from current directory
csvfiles = glob.glob('*.csv')

for csvfile in csvfiles:
    data = pd.read_csv(csvfile)
    x_or = list(range(len(data['Main Avg Power (mW)'])))
    y_or = list(data['Main Avg Power (mW)'])

    time=list(data['Time (s)'])
    rt=5000
    i=time[rt]
    k=i
    tlist=[]
    for i in time:
        tlist.append(y_or[rt])
    rt+=1
    if i-k>4:
        break
    idp=st.mean(tlist)
    sidp=st.stdev(tlist)
    newlist=[]
    imax=max(tlist)
    imin=min(min(tlist),idp-sidp)
    while imax>=y_or[rt]>=imin-1:
         newlist.append(y_or[rt])
         rt+= 1
    print(rt,"Mean idle power:",st.mean(newlist),"mW")
    midp=st.mean(newlist)

    # create new file name using the old one and adding '_new'
    newfile = csvfile.rstrip('.csv') + '_new.csv'    

    with open(newfile,'w',newline='') as f:
        thewriter=csv.writer(f)

        thewriter.writerow(['Idle Power(mW)'])
        thewriter.writerow([midp])