UnicodeDecodeError:'charmap'编解码器无法解码6554位的字节0x9d:字符映射到<undefined>

时间:2018-05-12 15:19:16

标签: python

我是python的新手而且我被困在编写用于打印CSV文件中任何特定行的代码。 CSV文件以表格格式包含以下数据。下面共享3行供参考(总数据有180行):

Case Number Case Title  Closed Date Yearmonth Closed    Owner Email Owner   Case Status Priority    Customer Name   Country Name    Created On  Created Yearmonth   Resolution time Greater than 50 Greater than 30
CAS-100828-Q5Z8 YYYY    04-09-2018  201804  ABC@ABC.COM abc Resolved    Major   Z-ESDE-RD-ER    IN  02-05-2018  201802  62.97109954 Yes Yes
CAS-103690-Z2Q9 XXXX    4/13/2018   201804  xyz@ABC.COM xyz Resolved    Major   Z-DERF-DSE-SS   JP  2/14/2018   201802  57.86796296 Yes Yes
CAS-105359-G9V1 ZZZZ    04-03-2018  201804  wxv@ABC.COM wxv Resolved    Minor   Z-EDRF-RDS-ED   PO  2/20/2018   201802  41.92416667 No  Yes

我想列出在任何列中具有任何特定值的数据(例如,如果'年月'关闭值是201804,那么它将被打印,否则不会。)下一步是打印任何列的数据特定的解决时间(比如大于10天)

下面给出了我尝试过的代码以及错误。 请帮助纠正我并告诉代码编写的方式

import csv

with open("data1.csv", 'r') as f:
    reader = csv.DictReader(f)
    rows = [row for row in reader if row['Yearmonth Closed'] == '201804']

for row in rows:
    print (row)
---------------------------------------------------------------------------
UnicodeDecodeError                        Traceback (most recent call last)
<ipython-input-49-6d59c793d8ac> in <module>()
      3 with open("data1.csv", 'r') as f:
      4     reader = csv.DictReader(f)
----> 5     rows = [row for row in reader if row['Yearmonth Closed'] == '201804']
      6 
      7 for row in rows:

<ipython-input-49-6d59c793d8ac> in <listcomp>(.0)
      3 with open("data1.csv", 'r') as f:
      4     reader = csv.DictReader(f)
----> 5     rows = [row for row in reader if row['Yearmonth Closed'] == '201804']
      6 
      7 for row in rows:

~\AppData\Local\Continuum\anaconda3-1\lib\csv.py in __next__(self)
    109         if self.line_num == 0:
    110             # Used only for its side effect.
--> 111             self.fieldnames
    112         row = next(self.reader)
    113         self.line_num = self.reader.line_num

~\AppData\Local\Continuum\anaconda3-1\lib\csv.py in fieldnames(self)
     96         if self._fieldnames is None:
     97             try:
---> 98                 self._fieldnames = next(self.reader)
     99             except StopIteration:
    100                 pass

~\AppData\Local\Continuum\anaconda3-1\lib\encodings\cp1252.py in decode(self, input, final)
     21 class IncrementalDecoder(codecs.IncrementalDecoder):
     22     def decode(self, input, final=False):
---> 23         return codecs.charmap_decode(input,self.errors,decoding_table)[0]
     24 
     25 class StreamWriter(Codec,codecs.StreamWriter):

UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 6554: character maps to <undefined>

0 个答案:

没有答案