LINUX.ORG.RU

проблема с кодировкой


0

1

помогите решить проблему с кодировкой. код на питоне:

#! /usr/bin/env python
#-*-coding: utf-8 -*-

from xml.etree.ElementTree import ElementTree

fileOut = open(r'NTRout.txt','w')

tree = ElementTree()
xmlDoc = tree.parse("NTR.xml")
links = list(xmlDoc.iter("ru-RU"))
size = len(links)

for i in range(0,size):
	a = links[i].text
	print type(a)
	fileOut.write(a.encode('utf-8')+' ')
	fileOut.write('\n')

xml файл который обрабатывается:

<?xml version="1.0" encoding="UTF-8"?>
<dynaForm type="xmlform" name="1351006344da576bad1be54064495323/3433171584ddb746a792c88064702951" width="700" enabletemplate="0" mode="edit" nextstepsave="" printdynaform="0">
  <title type="title" enablehtml="0" btn_cancel="Cancel">
    <ru-RU>Паспорт научно-технической разработки</ru-RU>
  
  </title>
  <date_filled type="text" maxlength="10" validate="Any" required="1" readonly="0" size="25" mode="edit" btn_cancel="Cancel" mask="" strto="" dependentfields="" defaultvalue="" hint="Формат ДД/ММ/ГГГГ " formula="" function="" sqlconnection="" savelabel="0">
    <ru-RU>Дата заполнения</ru-RU>
  </date_filled>
  <stitle1 type="subtitle" enablehtml="0" btn_cancel="Cancel" required="0" readonly="0" savelabel="0">
    <ru-RU>1. Название научно-технической разработки (НТР) [максимум 200 знаков]:</ru-RU>
  </stitle1>
  <nameof_pd type="textarea" required="1" readonly="0" rows="6" cols="72" mode="edit" btn_cancel="Отменить" defaultvalue="" hint="" sqlconnection="" savelabel="0">    <ru-RU/>
  </nameof_pd>
  <stitle2 type="subtitle" enablehtml="0" btn_cancel="Cancel" required="0" readonly="0" savelabel="0">
    <ru-RU>2. Краткая аннотация научно-технической разработки (основные цели и суть проекта, его уникальность, конкретное применение результатов проекта и их конкурентных преимуществ) [не более 500 знаков]:</ru-RU>
  </stitle2>
.......

Если убрать encode то ругается вот так:

<type 'unicode'>
Traceback (most recent call last):
  File "./newParser.py", line 18, in <module>
    fileOut.write(a+' ')
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-6: ordinal not in range(128)

если добавить encode('utf-8'):

<type 'unicode'>
<type 'unicode'>
<type 'unicode'>
<type 'NoneType'>
Traceback (most recent call last):
  File "./newParser.py", line 18, in <module>
    fileOut.write(a.encode('utf-8')+' ')
AttributeError: 'NoneType' object has no attribute 'encode'


Последнее исправление: samlabs821 (всего исправлений: 1)

Ответ на: комментарий от anonymous

В том то и дело что NoneType. Вопрос почему?

samlabs821
() автор топика

> помогите решить проблему с кодировкой. код на питоне:

Если бы код был на Common Lisp, то проблем бы не было в принципе.

anonymous
()
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.