【问题标题】:Need to convert Decode Strings that come from a DB需要转换来自数据库的解码字符串
【发布时间】:2016-09-19 06:13:40
【问题描述】:

美好的一天,

我有一个问题,下面的代码给了我以下结果。

'你好'] ['Unterl\xc3\xa4nderstr. 46'] ['Weilimdorf B\xc3\xbcrohaus'] ['物业名称'] ['你好在另一边'] ['Jahnstra\xc3\x9fe'] ['Bahnhofstr. 15'] ['卡尔斯鲁厄海峡。 3'] ['Bahnhofstra\xc3\x9fe 69'] ['Florians Haus'] ['Property Number 22'] ['Schickardstr.36'] ['Clichystr. 6'] ['属性名称 2'] ['Kirchstra\xc3\x9fe 5'] 无

我需要将 \xc3\xa4 显示为它们也对应但无法运行的 UTF-8 字母。我希望这里有人有类似的问题。

import cgi
import cgitb
cgitb.enable()
import sqlanydb

def db_dropdown():
  con = sqlanydb.connect( userid="DB", pwd="123", eng='DB',dbn='DB' )
  cursor = con.cursor()
  sql ="select distinct [Property] from Asset_Zielfonds"
  cursor.execute(sql)
  rowset = cursor.fetchall()
  encoded = [[s.encode('utf8') for s in t] for t in rowset]
  return encoded


def print_dropdown(data):  # Print the dropdown
    print '<div>'
    #print '<select>'
    for i in data:
        print '%s' % (i)
    #print '</select>'
    print '</div>'

print "Content-type:text/html\r\n\r\n"
print '<html>'
print '<meta charset="ISO-8859-1">'
print '<head>'
print '</head>'
print '<body>'
print '<h2>Retrieval of Data from DB</h2>'
print '<br>'
print '<br>'
print '<br>'
print print_dropdown(db_dropdown())
print '<br>'
print '<br>'
print '<br>'
print '</body>'
print '</html>'

【问题讨论】:

    标签: python-2.7 utf-8 cgi decode


    【解决方案1】:

    使用以下代码修复它。

    def db_dropdown():  # Execute query
        db = sqlanydb.connect( userid="", pwd="", eng='',dbn='' )  # Your DB details here
        cursor = db.cursor()
    
        sql ="select distinct [Property] from assets where  [Property] is not NULL"
    
        cursor.execute(sql)
        list_tested = cursor.fetchall()  # Get query response and store in variable
        list_tested = [i for sub in list_tested for i in sub]  # Convert to list from tuple
        return list_tested
    
    
    
    def print_dropdown(data):  # Print the dropdown
        print '<div>'
        print '<select>'
        for i in data:
          print '<option value="%s"selected>%s</option>' % (i.encode ('utf-8'), i.encode ('utf-8'))
        print '</select>'
        print '</div>'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-17
      相关资源
      最近更新 更多