簡體   English   中英

字符串格式python webapp2

[英]string formatting python webapp2

我只是從Google示例代碼留言簿中學習Python Webapp2,將逗號分隔的文本行存儲並顯示為格式化字符串。 我的問題是字符串中大約有11個項目,以逗號分隔,但是Python'%s'%或{}。 格式返回單行文本。 我試圖將文本轉換為tuple(),但分解為每個字符的列表會變得更糟。 我想念什么?

    for greeting in greetings:
        if greeting.author:
            self.response.write(
                    '<b>%s</b> wrote:' % greeting.author.nickname())
        else:
            self.response.write('Power consumption:')
        self.response.write('<blockquote>%s</blockquote>' %
                            cgi.escape(greeting.content))

        self.response.write(count: '{0}'.format(greeting.content.split(',')))

文本:

85327, '2000/0/0', '0:00:00',61.02,76.50,80.16,0.68,0.03,48.91, '二分之二千一百零六/ 6', '6點28分16秒'
計數:[u'85327',u“'2000/0/0'”,u“'0:00:00'”,u'61.02',u'76.50',u'80.16',u'0.68', u'0.03',u'48.91',u“'2106/2/6'”,u“'6:28:16'”]
留言簿名稱: 登錄

我認為您正在嘗試將它們分別放在單獨的一行上。 嘗試這個:

self.response.write('\\n'.join(greeting.content.split(',')))

這是什么:

  1. 將問候語字符串拆分為一個數組,其中每個元素都是逗號分隔的部分
  2. 將每個數組元素與一個\\n字符連接在一起。 這是換行符。

我不確定您在用count:做些什么count:因為那不是有效的python。

您將引號放錯了位置,請更改: self.response.write(count: '{0}'.format(greeting.content.split(',')))

至:

self.response.write('count: {0}'.format(greeting.content.split(',')))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM