时间数据'时间戳'与python中的格式不匹配?

时间:2017-03-21 10:50:12

标签: python csv

<html>

<head>
</head>

<body>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

  <table>
    <tbody>
      <tr>
        <th>Company</th>
        <th>Contact</th>
        <th>Country</th>
      </tr>
      <tr>
        <td>Alfreds Futterkiste</td>
        <td>Maria Anders</td>
        <td>Germany</td>
      </tr>
      <tr>
        <td>Alfreds Futterkiste</td>
        <td>Maria Anders</td>
        <td>Germany</td>
      </tr>
      <tr>
        <td>Alfreds Futterkiste</td>
        <td>Maria Anders</td>
        <td>Germany</td>
      </tr>
      <tr>
        <td>Centro comercial Moctezuma</td>
        <td>Francisco Chang</td>
        <td>Mexico</td>
      </tr>
      <tr>
        <td>Ernst Handel</td>
        <td>Roland Mendel</td>
        <td>Austria</td>
      </tr>
      <tr>
        <td>Island Trading</td>
        <td>Helen Bennett</td>
        <td>UK</td>
      </tr>
      <tr>
        <td>Laughing Bacchus Winecellars</td>
        <td>Yoshi Tannamuri</td>
        <td>Canada</td>
      </tr>
      <tr>
        <td>Magazzini Alimentari Riuniti</td>
        <td>Giovanni Rovelli</td>
        <td>Italy</td>
      </tr>
    </tbody>
  </table>

</body>

</html>

我已经运行了这段代码,而且时间戳出现了错误,我无法弄清楚它是什么, 帮帮我解决错误是什么?

错误:

> import datetime     
  import operator 
  import csv from itertools import islice
  with open( 'Path', 'r') as f:
  reader = csv.reader(f)
  output = list(islice((row[:2] for row in reader), 10))   #row[:2] for  columns and 10 for rows

#print (output)
ts = sorted([(datetime.datetime.strptime(j[0],  "%d-%m-%Y %H:%M:%S"),
j[1],j[2]) for j in [list(filter(None,k)) for k in output]], 
 key=operator.itemgetter(0)) h=[] for i in ts:
        if i[0].hour not in h:
                 print (i)
                 h.append(i[0].hour)

CSV:

时间戳,DATA1,DATA2
20-03-2017 10:00:01,50,60.5
20-03-2017 10:10:00,60,70
20-03-2017 10:40:01,75,80
20-03-2017 11:05:00,44,65
20-03-2017 11:25:01,98,42
20-03-2017 11:50:01,12,99
20-03-2017 12:00:05,13,​​54
20-03-2017 12:05:01,78,78
20-03-2017 12:59:01,15,89
20-03-2017 13:00:00,46,99
20-03-2017 13:23:01,44,45
20-03-2017 13:45:08,80,39

1 个答案:

答案 0 :(得分:0)

您正在处理csv中的标题行,您必须跳过它。尝试

output = list(islice((row[:2] for row in list(reader)[1:]), 10))

list(reader)[1:]基本上会提供除第一行以外的所有list(reader)