我有一些我一直在研究的代码,它采用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])
答案 0 :(得分:1)
问题是该输入文件中存在非标准的ascii字符,特别是具有十六进制值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'))]