简体   繁体   English

在Python中以普通文本打印元组数据

[英]Printing tuple data in normal text in Python

I have variable r=(u'East london,London,England', u'Mr.Baker in East london (at 2010-02-21 15:25:27.0)') in this format from webservice as a output from small program. 我从Web服务以这种格式从小程序输出了r=(u'East london,London,England', u'Mr.Baker in East london (at 2010-02-21 15:25:27.0)') How can I print these tuple data as normal string like: 如何将这些元组数据打印为普通字符串,例如:

East london,London,England   Mr.Baker in East london (at 2010-02-21 15:25:27.0) 

can anybody help me out of this please?Thanks in advance! 有人可以帮我这个忙吗?谢谢!

my code is giving now! 我的代码正在提供!

from sqlite3 import *

import feedparser
import codecs# newly added


data = feedparser.parse("some url")


conn = connect('location2.db')
curs = conn.cursor()

curs.execute('''create table location_top6
  ( id integer primary key,title text ,
        updated text)''')


for i in range(len(data['entries'])):
    curs.execute("insert into location_top6 values\
        (NULL, '%s', '%s')" % (data.entries[i].title,data.entries[i].summary))

conn.commit()
curs.execute("select * from location_top6")
for r in curs:
    print r

and I want this r value printed as normal string! 我希望将此r值打印为普通字符串!

just join on the separator, if it's space it would be: 只需加入分隔符,如果有空格,它将是:

' '.join(r)

edit : re your update code. 编辑 :重新输入您的更新代码。 Your table contains primary key, as can be seen from the table definition, that primary key is an integer. 从表定义可以看出,表包含主键,该主键是整数。 That's why you're getting that TypeError . 这就是为什么要获取那个TypeError的原因。 The question is whether you want to print that primary key or not. 问题是您是否要打印该主键。 If the answer is yes you could do the following: 如果答案是肯定的,则可以执行以下操作:

' '.join(str(i) for i in r)

if no is the answer: you just need to ' '.join(r[1:]) . 如果没有答案, ' '.join(r[1:])需要' '.join(r[1:])

如果序列x包含字符串以外的其他类型,请首先将其转换:

' '.join( map( str, x ) )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM