2017-03-03 130 views
-1
import glob 
from datetime import date 
import csv 
import numpy as np 
import pandas as pd 
from itertools import islice 
path = 'C:/Users/Calum/AppData/Local/Programs/Python/Python35-32/Python Programs/Brunel Forecasts/November/*.csv' 
for fname in glob.glob(path): 
    with open (fname) as f: 
     readCSV = csv.reader ((islice(f, 24, None)), delimiter = ';') 
     irrad_fore = [] 
     timestamp = [] 
     day = [] 
     month = [] 
     year = [] 
     for row in readCSV: 
      irrad1 = row[4] 
      time = row[3] 
      d = row[2] 
      m = row[1] 
      y = row[0] 

      irrad_fore.append(irrad1) 
      timestamp.append(time) 
      day.append(d) 
      month.append(m) 
      year.append(y) 

      out = csv.writer(open('C:/Users/Calum/AppData/Local/Programs/Python/Python35-32/Python Programs/Brunel Forecasts/forecast_11-2016.csv','w', newline=''), delimiter=';') 
      for values in zip(year, month, day, timestamp, irrad_fore): 
       x = values 
       out.writerow(x) 

为什么输出csv文件只包含最后几天的数据,因为当我刚刚将变量x打印到屏幕上时,它包含整个月份的数据?为什么输出csv文件不显示预期结果?

回答

0

编辑:

在我看来,那是你要实现的目标(基本串连所有文件?)是这样的:

path = 'C:/Users/Calum/AppData/Local/Programs/Python/Python35-32/Python Programs/Brunel Forecasts/November/*.csv' 

irrad_fore = [] 
timestamp = [] 
day = [] 
month = [] 
year = [] 

for fname in glob.glob(path): 
    with open (fname) as f: 
     readCSV = csv.reader ((islice(f, 24, None)), delimiter = ';') 

     for row in readCSV: 
      irrad1 = row[4] 
      time = row[3] 
      d = row[2] 
      m = row[1] 
      y = row[0] 

      irrad_fore.append(irrad1) 
      timestamp.append(time) 
      day.append(d) 
      month.append(m) 
      year.append(y) 

out = csv.writer(open('C:/Users/Calum/AppData/Local/Programs/Python/Python35-32/Python Programs/Brunel Forecasts/forecast_11-2016.csv','w', newline=''), delimiter=';') 
for values in zip(year, month, day, timestamp, irrad_fore): 
    x = values 
    out.writerow(x) 

虽然这将是可能更好写CSV文件直接没有汇总到临时列表中的所有内容:

path = 'C:/Users/Calum/AppData/Local/Programs/Python/Python35-32/Python Programs/Brunel Forecasts/November/*.csv' 
out = csv.writer(open('C:/Users/Calum/AppData/Local/Programs/Python/Python35-32/Python Programs/Brunel Forecasts/forecast_11-2016.csv','w', newline=''), delimiter=';') 

for fname in glob.glob(path): 
    with open (fname) as f: 
     readCSV = csv.reader ((islice(f, 24, None)), delimiter = ';') 

     for row in readCSV: 
      irrad1 = row[4] 
      time = row[3] 
      d = row[2] 
      m = row[1] 
      y = row[0] 

      out.writerow([y,m,d,time,irrad1]) 
+0

谢谢,但解决问题,但是当我将语句移动到开始或之前的状态它会导致输出的顺序完全混乱,我不明白为什么? –

+0

@ C.Deakin我不确定我是否理解你的问题 - 你是否试图基本连接csv文件?我已更新答案以反映此... – ewcz

相关问题