0
我的分析器功能使用lxml
並提供了一個Unicode字符串列表(book_list
)。Python unicode問題subprocess.call
將這些字符串連接在一起形成文件名,清理後通過subprocess.call
傳遞給另一個繼續工作的二進制文件。
我的問題是,unicode對象(例如title_name = u'Wunderlicher Traum von einem gro\xdfen Narrennest'
)在ISO-8859-2中編碼(至少這就是'chardet'告訴我的),我需要將它們轉換爲格式,系統級別。當前的代碼導致文件名爲u'Wunderlicher Traum von einem gro\xc3\x9fen Narrennest'
。
有沒有人有想法我做錯了什麼?
一些相關信息:
sys.getdefaultencoding()
回報ascii
,這讓我困惑,因爲理論上不應該讓像AOU等任何特殊字符)。- OS X 10.9,Python的2.7.5
def convert_books(book_list, output_dir):
for book in book_list:
author_name = book[0][0]
title_name = book[0][1]
#print chardet.detect(title_name)
#print type(title_name)
#print title_name.decode('iso-8859-2')
year_name = "1337"
output_file = u"%s - %s (%s).pdf" % (author_name, title_name, year_name)
keep_characters = (' ', '.', '_')
output_file.join(c for c in output_file if c.isalnum() or c in keep_characters).rstrip()
path_to_out = "%s%s" % (output_dir, output_file)
target_file = WORK_DIR + book[1].replace(".xml", ".html")
engine_parameter = [
WKHTMLTOPDF_BIN,
# GENERAL
"-l", # lower quality
"-L", "25mm",
"-R", "25mm",
"-T", "25mm",
"-B", "35mm",
"--user-style-sheet", "media/style.css",
target_file,
path_to_out,
]
print "+ Creating PDF \"%s\"" % (output_file)
call(engine_parameter)