What to do if when reading the list - the characters are readable, and after converting it to a string - lose their readability? What to do with the string? Teach

For example:

for i in range(0, len(fullTEXT)): print fullTEXT[i].text textList.append(fullTEXT[i].text) 

Известно, что нервные клетки

 fullTEXT = str(textList) print fullTEXT 

[u'\u0418\u0437\u0432\u0435\u0441\u0442\u043d\u043e, \u0447\u0442\u043e \u043d\u0435\u0440\u0432\u043d\u044b\u0435 \u043a\u043b\u0435\u0442\u043a\u0438']

  • Go to the third python, there is no such problem) - andreymal
  • @andreymal, thanks, I will know. And there is no solution for 2.x? - 0x0718
  • It's a shame, everything got up because of this. I understand the UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-4: ordinal not in range(128) error UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-4: ordinal not in range(128) too, because of this. - 0x0718
  • And what did something get up for? And the second mistake is a consequence of an incomplete understanding of the work of the lines, probably - andmalmal
  • 2
    @andreymal: Python 3, where repr() may show some Unicode characters without an '\uhhhh' escapes, only makes the problem less obvious. In Python 3, I watched people start complaining in such situations: "why did the characters appear in my text ['',] "(or worse: .replace('[', '') etc. would start appearing in the code) . The real problem is not related to the Python version, but to the lack of understanding of the difference between obj and repr(obj) . - jfs

2 answers 2

If your goal is to get one line from the list of lines stored in the list, you can do something like:

 >>>fullTEXT = [u'Известно, что нервные клетки', u'не восстанавливаются'] >>>print ' '.join(fullTEXT) Известно, что нервные клетки не восстанавливаются 

If the goal is different, describe what you want to achieve by performing:

 fullTEXT = str(textList) print fullTEXT 

Council Your cycle:

 for i in range(0, len(fullTEXT)): print fullTEXT[i].text textList.append(fullTEXT[i].text) 

can be simplified by removing len and range:

 for item in fullText: print item.text textList.append(item.text) 
  • And textList += [item.text for item in fullText] will be even steeper. - awesoon

You can like this:

 >>> mylist = [u"Известно, что нервные клетки", u"Что-то совсем другое"] >>> print u'[%s]' % ', '.join(['"%s"' % item for item in mylist]) ["Известно, что нервные клетки", "Что-то совсем другое"]