1
我不知道我出錯的地方,但是當我試圖寫入一個excel文件時,數據庫值顯示上述錯誤。ascii'編解碼器無法編碼字符u' xa0'在位置0:序號不在範圍內(128)
在我views.py
ast = Asset.objects.all().exclude(id__in = list)
for i in ast:
print "kkkkkkkkkkkkkkkk",i.id
dic1={}
dic1['AssetID'] = i.id
dic1['Assets'] = i.name
dic1['AssetCode'] = i.asset_code
dic1['ModelNo'] = i.model_no
dic1['Barcode'] = i.barcode
dic1['SerialNo'] = i.serial_no
dic1['AssetClass'] = i.asset_class
dic1['AssetStatus'] = i.asset_status
dic1['Price'] = i.price
dic1['AssetCategory'] = i.asset_category
dic1['Warranty'] = i.warranty
dic1['CreatedDate'] = i.created_date
dic1['Employee'] = "Unassigned"
dic1['SubLocation'] = "Unassigned"
dic1['Department'] = "Unassigned"
dic1['Dateofassign'] ="Unassigned"
list1.append(dic1)
import csv
print settings.MEDIA_ROOT+'\\'+str(2)+'.csv'
writer = csv.writer(open(settings.MEDIA_ROOT+'\\'+str(2)+'.csv', 'wb'))
writer.writerow(['','','','Asset Reports','',''])
writer.writerow(['Asset ID', 'Assets', 'Asset Code', 'Model No.', 'Barcode', 'Serial No.', 'Asset Class', 'Asset Status', 'Price', 'Asset Category', 'Warranty', 'Created Date','Employee','SubLocation','Department','Date of assign'])
listobj = list1.decode('ascii', 'ignore')
for i in range(len(listobj)):
writer.writerow([ listobj[i]['AssetID'], listobj[i]['Assets'], listobj[i]['AssetCode'], listobj[i]['ModelNo'], listobj[i]['Barcode'], listobj[i]['SerialNo'], listobj[i]['AssetClass'], listobj[i]['AssetStatus'], listobj[i]['Price'], listobj[i]['AssetCategory'], listobj[i]['Warranty'], listobj[i]['CreatedDate'],listobj[i]['Employee'],listobj[i]['SubLocation'],listobj[i]['Department'], listobj[i]['Dateofassign']])
我應該怎麼做。
我的追蹤誤差:
Traceback:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" ,111,in get_response
response = callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Gopi\asset_track1\asset_mgmt\reg_views.py" in Locsrchs
699. writer.writerow([ listobj[i]['AssetID'], listobj[i]['Assets'], listobj[i]['AssetCode'], listobj[i]['ModelNo'], listobj[i]['Barcode'], listobj[i]['SerialNo'], listobj[i]['AssetClass'], listobj[i]['AssetStatus'], listobj[i]['Price'], listobj[i]['AssetCategory'], listobj[i]['Warranty'], listobj[i]['CreatedDate'],listobj[i]['Employee'],listobj[i]['SubLocation'],listobj[i]['Department'], listobj[i]['Dateofassign']])
Exception Type: UnicodeEncodeError at /asset_track1/locsrch2/
Exception Value: 'ascii' codec can't encode character u'\xa0' in position 0: ordinal not in range(128)
什麼是list1?它的decode()方法應該做什麼? – scav
list1是空列表,並忽略解碼行,因爲我正在嘗試一些東西,我粘貼它,解碼線沒有更多use.just listobj = list1多數民衆贊成它就是這樣 –