尤其是在json load的時候,字符串中的不可見控制字符可能會導致錯誤,應該先對字符串進行控制字符過濾。
對網頁文本同樣適用,最好在處理網頁文本時先進性控制字符清洗。
Replace null bytes in html text with space char to walk around lxml bug in _convert_tree func
import re def remove_control_chars(s): control_chars = ''.join(map(unichr, range(0,32) + range(127,160))) control_char_re = re.compile('[%s]' % re.escape(control_chars)) return control_char_re.sub('', s) cleaned_json = remove_control_chars(original_json) obj = simplejson.loads(cleaned_json)
參考自:http://stackoverflow.com/questions/21495598/simplejson-encoding-issue-illegal-character
