2013-04-11 79 views
1

我有一些代碼,我一直在努力,需要一個csv文件並將其轉換爲適當填充的xls文件。 csv文件是從客戶網站發給我的。我已經在他們的8個csv文件上測試過了。前7名工作正常,我做了小小的調整,以提高效率。這個最新的csv導致了一個錯誤。當我嘗試以前的7他們工作正常。我得到的錯誤是:ascii'編解碼器無法解碼字節...需要幫助解決輸入差異

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行。當我刪除所有的記錄時,它只能工作。這導致我相信有東西被輸入導致錯誤。爲了解釋這一點,我在每個將數據從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]) 

回答

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'))] 
+0

嘿,我詢問與開發商。他們告訴我它是用'Unicode'編碼的。當我輸入encoding =「Unicode」時出現錯誤。 'LookupError:unknown encoding:Unicode'。我試着用大寫的U和小寫字母u。當我嘗試使用encoding =「utf-8」時,出現錯誤'UnicodeEncodeError:'ascii'編解碼器無法在位置130編碼字符u'\ u2013':序號不在範圍(128)'中。當我告訴他們他們正在編碼它們時,他們似乎不知所措。不知道他們只是不給我正確的答案,或者我錯過了一些東西。 – ExperimentsWithCode 2013-04-15 18:54:40

+0

@ExperimentsWithCode Unicode不是一種編碼。也許他們的意思是'utf-16'。但如果是這樣的話,我預計它會在位置130之前失敗。他們可能使用了一些ASCII代碼頁而沒有意識到它。我會發現那個角色應該是什麼,並找到一個可行的頁面。無論如何,一個很好的參考是http://docs.python.org/2/howto/unicode.html – cmd 2013-04-15 21:39:08

+0

也http://docs.python.org/2/library/codecs.html#standard-encodings – cmd 2013-04-15 21:48:23

相關問題