开发者

Printing a list using python

开发者 https://www.devze.com 2023-02-19 04:02 出处:网络
I have a list compiled from excel cells, using python - say listlist. Each element in the cell/list开发者_如何学编程 is in unicode. When I print the list as

I have a list compiled from excel cells, using python - say listlist. Each element in the cell/list开发者_如何学编程 is in unicode. When I print the list as

print listlist

I see 'u' prepended to each member of the list. But when I print the list like

for member in listlist:
  print member

I do not see the 'u' prepended to the member.

Can someone please explain to me why there is this difference? Is it defined in the xlrd module?


This is because print list is equivalent to

print "[", ", ".join(repr(i) for i in list), "]"

repr(s) is u"blabla" for a unicode string while print s prints only blabla.


When printing the list, it shows each object within the list using the object's __repr__

When printing the object alone, it uses the object's __str__


There are two ways to turn a Python value into a string str and repr (and their equivalent class methods __str__ and __repr__). The difference comes from these two functions. From the Python tutorial:

The str() function is meant to return representations of values which are fairly human-readable, while repr() is meant to generate representations which can be read by the interpreter (or will force a SyntaxError if there is not equivalent syntax).

The print statement calls str on the object you pass in, but the list's __str__ method calls repr on it's values. The values of your list are unicode strings. In human readable form the fact it's unicode is irrelevent, so you don't get a u, or even quote marks. In computer readable form it is important, so the u is included.


when you are printing a list, python prints a representation of the list.

the default representation of a list is to print an opening square brackets ([), then the representation of each element separated by a comma (,), the a closing square bracket (]). (to be precise the representation of an object is what is returned when calling its __repr__() member). note that the default representation of a unicode string is u'...'.

now, when you are printing a string, you are not printing the representation of the string, you are printing the string (the value returned by calling the __str__() member). and the string does not include formatting charaters like unicode flags, quotes, etc...

0

精彩评论

暂无评论...
验证码 换一张
取 消