2013-03-11 89 views
0

我想從使用Beautifulsoup從URL解析的字符串中移除磅符號。磅符號出現以下錯誤。 SyntaxError:文件中的非ASCII字符'\ xa3'在函數中使用£並將其寫入Python的csv中

我試圖把這個# -*- coding: utf-8 -*-放在類的開頭,但仍然出現錯誤。

這是代碼。獲得浮點數後,我想將其寫入csv文件。

mainTag = SoupStrainer('table', {'class':'item'}) 
    soup = BeautifulSoup(resp,parseOnlyThese=mainTag) 
    tag= soup.findAll('td')[3] 
    price = tag.text.strip() 

    pr = float(price.lstrip(u'£').replace(',', '')) 
+1

您可以用字符串中的\ xa3替換£。 – Vortico 2013-03-11 04:00:57

回答

0

該問題可能是編碼和字節與字符之一。 CSV文件使用什麼編碼?發生£符號的文件中有哪些字節序列?變量price中包含的字節是什麼?您需要替換字符串中實際發生的字節。其中一個難題就是源代碼中數據的內容。這就是源代碼頂部的# -*- coding: utf-8 -*-標記顯着的地方:它告訴python如何解釋字符串文本中的字節。在替換字符之前,您可能需要(或想要)解碼CSV文件中的字節以創建Unicode字符串。

我將指出documentation for the csv module in Python 2.7說:

Note: This version of the csv module doesn’t support Unicode input. Also, there are currently some issues regarding ASCII NUL characters. Accordingly, all input should be UTF-8 or printable ASCII to be safe; see the examples in section Examples.

的實施例部分包括以下代碼演示通過csv模塊以Unicode字符串提供的字節進行解碼。

import csv 

def unicode_csv_reader(unicode_csv_data, dialect=csv.excel, **kwargs): 
    # csv.py doesn't do Unicode; encode temporarily as UTF-8: 
    csv_reader = csv.reader(utf_8_encoder(unicode_csv_data), 
         dialect=dialect, **kwargs) 
    for row in csv_reader: 
     # decode UTF-8 back to Unicode, cell by cell: 
     yield [unicode(cell, 'utf-8') for cell in row] 
相關問題