Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 打印列表时使用括号_Python_List_Tuples_Python Unicode - Fatal编程技术网

Python 打印列表时使用括号

Python 打印列表时使用括号,python,list,tuples,python-unicode,Python,List,Tuples,Python Unicode,这不是特定于sqlite的,但在学习python和sqlite3并列出查询结果时,我想知道这一点。我有简单的代码: connection = sqlite3.connect("db.sqlite") cursor = connection.cursor() cursor.execute("select * from db") print cursor.fetchall() 因此,print cursor.fetchall()的结果是:[(u'koko',),(u'lolo',)] 但是,当我尝

这不是特定于sqlite的,但在学习python和sqlite3并列出查询结果时,我想知道这一点。我有简单的代码:

connection = sqlite3.connect("db.sqlite")
cursor = connection.cursor()
cursor.execute("select * from db")
print cursor.fetchall()
因此,
print cursor.fetchall()
的结果是:
[(u'koko',),(u'lolo',)]
但是,当我尝试使用此代码重新创建此类打印时:

i=["koko","lolo"]
print i
打印结果是:
['koko','lolo']

有两件事我不明白:

  • 为什么第一个列表在打印时有unicode的“u”,而第二个没有
  • 为什么打印时第一个列表有括号
    (u'koko',)
    ,而第二个没有

  • 第一个列表可能是元组列表吗?

    列表中有括号,因为python就是这样打印元组的

    对比度:

    a = 'foo'
    print a,type(a)
    
    与:

    还要注意,在python2.x中,有不同类型的字符串
    unicode
    只是一种字符串类型,python选择将其表示为
    u'whatever'

    >>> print u'whatever'
    whatever
    >>> print repr(u'whatever')
    u'whatever'
    

    请注意,
    str(tup)
    对元组中的每个元素隐式调用
    repr

    列表中有括号,因为python就是这样打印元组的

    对比度:

    a = 'foo'
    print a,type(a)
    
    与:

    还要注意,在python2.x中,有不同类型的字符串
    unicode
    只是一种字符串类型,python选择将其表示为
    u'whatever'

    >>> print u'whatever'
    whatever
    >>> print repr(u'whatever')
    u'whatever'
    
    请注意,
    str(tup)
    对元组中的每个元素隐式调用
    repr

    [(u'koko',),(u'lolo',)]
    是unicode字符串的单元素元组列表

    [“koko”,“lolo”]
    是字节字符串的列表。

    [(u'koko',),(u'lolo',)]
    是unicode字符串的单元素元组列表


    [“koko”,“lolo”]
    是一个字节字符串列表。

    您可能需要操纵结果以使其适应所需的格式

    rows = cursor.fetchall()
    result = [elem[0] for elem in rows]
    

    …并且可能对元素[0]应用一个函数(取决于您的python版本),以将字符串设置为您想要的格式。

    您可能需要操纵结果以使其适应您想要的格式

    rows = cursor.fetchall()
    result = [elem[0] for elem in rows]
    

    …并且可能对元素[0]应用一个函数(取决于您的python版本),以将字符串设置为您想要的格式。

    @cikatomo您可以使用
    u'some string'
    作为文本,或使用
    unicode()
    内置命令将字符串设置为unicode文本。或者,使用Python3.x,默认情况下所有字符串都是Unicode。值得注意的是,一般来说,unicode字符串和字节字符串可以在其他字符串可以工作的任何地方工作(除了少数情况下,或者自然地,在需要unicode字符的地方)。如果你想做同样的事情,为什么不复制它呢?
    repr(x)
    的要点是,如果将结果字符串粘贴到代码中,它将生成
    x
    。(给定正确的导入和正确实现
    repr()
    的类)。@cikatomo您可以使用
    u'some string'
    作为文本,或使用内置的
    unicode()
    使字符串成为unicode文本。或者,使用Python3.x,默认情况下所有字符串都是Unicode。值得注意的是,一般来说,unicode字符串和字节字符串可以在其他字符串可以工作的任何地方工作(除了少数情况下,或者自然地,在需要unicode字符的地方)。如果你想做同样的事情,为什么不复制它呢?
    repr(x)
    的要点是,如果将结果字符串粘贴到代码中,它将生成
    x
    。(给定正确的导入和正确实现
    repr()
    的类)。