1

我有一些我一直在处理的代码,它需要一个 csv 文件并将其转换为一个适当填充的 xls 文件。csv 文件是从客户网站发送给我的。我已经在他们的 8 个 csv 文件上对其进行了测试。前 7 个工作正常,我做了一些小调整以提高它的效率。这个最新的 csv 导致了一个错误。当我尝试前 7 个时,它们运行良好。我得到的错误是:

  Traceback (most recent call last):
  File "/Users/USER/Documents/PYTHON/IFG User Update/code/ifg_user_update.py", line 232, in <module>
    wb.save((os.path.expanduser("~/Downloads/Users_IFG_PTI")) + '.out.' + os.path.splitext((os.path.expanduser("~/Downloads/Users_IFG_PTI.xls")))[-1])
  File "/usr/local/lib/python2.7/site-packages/xlwt/Workbook.py", line 643, in save
    doc.save(filename, self.get_biff_data())
  File "/usr/local/lib/python2.7/site-packages/xlwt/Workbook.py", line 618, in get_biff_data
    shared_str_table   = self.__sst_rec()
  File "/usr/local/lib/python2.7/site-packages/xlwt/Workbook.py", line 580, in __sst_rec
    return self.__sst.get_biff_record()
  File "/usr/local/lib/python2.7/site-packages/xlwt/BIFFRecords.py", line 77, in get_biff_record
    self._add_to_sst(s)
  File "/usr/local/lib/python2.7/site-packages/xlwt/BIFFRecords.py", line 92, in _add_to_sst
    u_str = upack2(s, self.encoding)
  File "/usr/local/lib/python2.7/site-packages/xlwt/UnicodeUtils.py", line 50, in upack2
    us = unicode(s, encoding)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 38: ordinal not in range(128)

第 8 个 csv 中有 90 行。当我删除除 1 条记录之外的所有记录时,它起作用了。这让我相信有一些输入导致错误。为了解决这个问题,我在将数据从 csv 写入 xls 的每个实例周围添加了字符串标记。这次尝试失败了。当我查找其他答案时,他们通常处理 encode() 标签。我不直接使用其中的任何一个,尽管被调用的模块可能会使用它们。

有什么建议吗?谢谢你。

下面是我的代码。我正在使用 Python 2.7、Mac os x 10.8.2、

#!/usr/bin/env python
## Import OS and Modules
import os
import csv
import xlrd
import xlwt
import xlutils
import csv
import collections

## Define Input File from IFG
ifg_user_file = "New_PCs_to_set_up_in_marketing_database_-_4-11-2013.csv"

## Import data
data = [row for row in csv.reader(open (os.path.expanduser("~/Downloads/" + ifg_user_file),'U'))]

## Find number of rows
row_count = sum(1 for row in data)
print row_count
## Set to turn off when reaching the end of data
end_of_data = False
## Repeat user skips the write to excel portion
repeat_user = False
## y = Row to check. Start with row 2. Skip header row. 
y=1
## Set empty list of good y values that do not repeat a user
good_y=[]
## Repeat until all data has been observed
while y < row_count:
    ## Open Existing Users list for comparison
    existing_users = open (os.path.expanduser("~/Documents/PYTHON/IFG User Update/ExistingUsersList.txt"),'r')
    ## Check potential new username against existing users list
    for line in existing_users.readlines():
        if (data[y][2]+'\n') == line:
            repeat_user = True
            break
##        print line
##        print data[y][2]
##        print repeat_user
    existing_users.close()
    ## If the user is not a repeat, add user to the list. 
    if repeat_user == False:
        existing_users = open (os.path.expanduser("~/Documents/PYTHON/IFG User Update/ExistingUsersList.txt"),'a')
        existing_users.write('\n' + data[y][2])
        existing_users.close()
        good_y.append(y)

    else:
        repeat_users_list = open (os.path.expanduser("~/Documents/PYTHON/IFG User Update/ExistingUsersList.txt"),'a')
        print "___________Repeated User_______________"
        print data[y][2]
        print "_______________________________________"
    repeat_user = False
    y+=1
    print y

#### Set New Variables #####################################
##number_of_add_users = len(good_y)
##first=[]  # x=0
##last=[]   # x=1
##email=[]  # x=2
##group=[]  # x=3
##company_web=[] # x=4
##office_phone=[] # x=5
##business_name=[]  # x=6
##address=[] # x=7
##address2=[] # x=8
##city=[] # x=9
##state=[] # x=10
##zipcode=[] # x=11
##x=0

from xlutils.copy import copy # http://pypi.python.org/pypi/xlutils
from xlrd import open_workbook # http://pypi.python.org/pypi/xlrd
from xlwt import easyxf # http://pypi.python.org/pypi/xlwt

rb = open_workbook((os.path.expanduser("~/Documents/PYTHON/IFG User Update/Users_IFG_PTI.xls")),formatting_info=True)
r_sheet = rb.sheet_by_index(0) # read only copy to introspect the file
wb = copy(rb) # a writable copy 
w_sheet = wb.get_sheet(0) # the sheet to write to within the writable copy

## Set starting point
xls_row = 7
xls_column = 0
for user in good_y:
    ## Write User Name
    w_sheet.write(xls_row, xls_column, data[user][2])
    xls_column += 1
    ## Write First Name
    w_sheet.write(xls_row, xls_column, data[user][0])
    xls_column += 1
    ## Write Last Name
    w_sheet.write(xls_row, xls_column, data[user][1])
    xls_column += 1
    ## Write Email
    w_sheet.write(xls_row, xls_column, data[user][2])
    xls_column += 1
    ## Write Password
    w_sheet.write(xls_row, xls_column, "XXXXXXXXXXXXX")
    xls_column += 1
    ## Write Company Name
    w_sheet.write(xls_row, xls_column, data[user][6])
    xls_column += 1
    ## Write User Type
    w_sheet.write(xls_row, xls_column, "Purchaser")
    xls_column += 1
    ## Write Active
    w_sheet.write(xls_row, xls_column, "Active")
    xls_column += 1
    ## Write Show Payment Options
    w_sheet.write(xls_row, xls_column, "Yes")
    xls_column += 1
    ## Write Payment Methods
    w_sheet.write(xls_row, xls_column, "Inherit from Group")
    xls_column += 2
    ## Write Calculate Sales Tax
    w_sheet.write(xls_row, xls_column, "Yes")
    xls_column += 1
    ## Write Filter
    w_sheet.write(xls_row, xls_column, "No")
    xls_column += 1
    ## Write Cost Center Options
    w_sheet.write(xls_row, xls_column, "hide cost centers")
    xls_column += 2
    ## Write Profile Admin
    w_sheet.write(xls_row, xls_column, "No")
    xls_column += 1
    ## Write Enable Edit User
    w_sheet.write(xls_row, xls_column, "Edit Password Only")
    xls_column += 1
    ## Write Use Existing Address section for shipping
    w_sheet.write(xls_row, xls_column, "Inherit From Usergroup")
    xls_column += 1
    ## Write Use Existing Address section for billing
    w_sheet.write(xls_row, xls_column, "Inherit From Usergroup")
    xls_column += 1
    ## Write User Group and Group ID
    ## Special section, determine group info based on column 3
    if data[user][3] == "xxxxxxxxxxxx":
        w_sheet.write(xls_row, xls_column, "xxxxxxxxxxxx")
        w_sheet.write(xls_row, (xls_column+1), "xxxxxxxxxxxx")
    elif data[user][3] == "xxxxxxxxxxxx":
        w_sheet.write(xls_row, xls_column, "xxxxxxxxxxxx")
        w_sheet.write(xls_row, (xls_column+1), "xxxxxxxxxxxx)
    elif data[user][3] == "xxxxxxxxxxxx":
        w_sheet.write(xls_row, xls_column, "xxxxxxxxxxxx")
        w_sheet.write(xls_row, (xls_column+1), "xxxxxxxxxxxx)
    elif data[user][3] == "xxxxxxxxxxxx":
        w_sheet.write(xls_row, xls_column, "xxxxxxxxxxxx")
        w_sheet.write(xls_row, (xls_column+1), "xxxxxxxxxxxx")
    elif data[user][3] == "xxxxxxxxxxxx":
        w_sheet.write(xls_row, xls_column, "xxxxxxxxxxxx")
        w_sheet.write(xls_row, (xls_column+1), "xxxxxxxxxxxx")
    elif data[user][3] == "xxxxxxxxxxxx":
        w_sheet.write(xls_row, xls_column, "xxxxxxxxxxxx")
        w_sheet.write(xls_row, (xls_column+1), "xxxxxxxxxxxx")
    elif data[user][3] == "xxxxxxxxxxxx":
        w_sheet.write(xls_row, xls_column, "xxxxxxxxxxxx")
        w_sheet.write(xls_row, (xls_column+1), "xxxxxxxxxxxx")
    elif data[user][3] == "xxxxxxxxxxxx":
        w_sheet.write(xls_row, xls_column, "xxxxxxxxxxxx")
        w_sheet.write(xls_row, (xls_column+1), "xxxxxxxxxxxx")
    elif data[user][3] == "xxxxxxxxxxxx":
        w_sheet.write(xls_row, xls_column, "xxxxxxxxxxxx")
        w_sheet.write(xls_row, (xls_column+1), "xxxxxxxxxxxx")
    elif data[user][3] == "xxxxxxxxxxxx":
        w_sheet.write(xls_row, xls_column, "xxxxxxxxxxxx")
        w_sheet.write(xls_row, (xls_column+1), "xxxxxxxxxxxx")
    else:
        w_sheet.write(xls_row, xls_column, "No Such User Group")
        w_sheet.write(xls_row, (xls_column+1), "Error")
    xls_column += 2
    ## Write Reset Last Activity Date
    w_sheet.write(xls_row, xls_column, "No")
    xls_column += 1
    ## Write Force Password Reset
    w_sheet.write(xls_row, xls_column, "No")
    xls_column += 1
    ## Write Edit Image Collections
    w_sheet.write(xls_row, xls_column, "No")
    xls_column += 1
    ## Write Impersonate Group ID
    w_sheet.write(xls_row, xls_column, "none")
    xls_column += 1
    ## Write Local
    w_sheet.write(xls_row, xls_column, "Inherit")
    xls_column += 1
    ## Reset for next user
    xls_row += 1
    xls_column = 0
    print "updated "+ str(data[user][2])

## Save output copy
wb.save((os.path.expanduser("~/Downloads/Users_IFG_PTI")) + '.out.' + os.path.splitext((os.path.expanduser("~/Downloads/Users_IFG_PTI.xls")))[-1])
4

1 回答 1

1

问题是该输入文件中有一个非标准的 ascii 字符,特别是带有 hex value 的字符0xe2。如果您有非标准的 ascii,您需要删除有问题的字符或根据文件的实际编码(如 utf-8 或 iso-8859-15 或其他东西)对其进行解码。

例如

import codecs

filename = os.path.expanduser("~/Downloads/" + ifg_user_file)
data = [row for row in csv.reader(codecs.open(filename, 'U', encoding='utf-8'))]
于 2013-04-11T20:04:38.307 回答