Найти - Пользователи
Полная версия: Декодирование русского текста ASCII формата из RTF файла
Начало » Python для новичков » Декодирование русского текста ASCII формата из RTF файла
1
monster_kot
Здравствуйте!
Помогите разобраться..

Если пишу так
st = '\xd0\xe0\xf1\xf7\xe5\xf2\xed\xfb\xe5 \xeb\xe8\xf1\xf2\xea\xe8'
st = st.decode('cp1251')
print st
Переменной st присваивается значение “Расчетные листки”

Если формирую строку для конвертации программно, получая ее из RTF файла:
st = header[header.find('{\\title ')+8:header.find('}', header.find('{\\title ')+8)].replace('\\\'', '\\x')
st = st.decode('cp1251')
print st
Переменной st присваивается значение “\xd0\xe0\xf1\xf7\xe5\xf2\xed\xfb\xe5 \xeb\xe8\xf1\xf2\xea\xe8”
monster_kot
Решение нашел!
def rtf_StrConv(instr):
   ret_str = ''
   i = 0
   while i < len(instr):
       if instr[i:i+2] == '\\\'':
           ret_str = ret_str+instr[i+2:i+4].decode('hex').decode('cp1251')
           i = i+4
       else:
           ret_str = ret_str+instr[i]
           i = i+1
   return ret_str
print rtf_StrConv(header[header.find('{\\title ')+8:header.find('}', header.find('{\\title ')+8)])
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB