2013-01-04 60 views
1

我交流編碼器,但是Python是differrent,我遇到了一個問題,關於這個如何在...中連接字符串? (蟒蛇)

items = [ 
    ["/ank/homepage.py","Home"], 
    ["/ank/package.py","Packages"], 
    ["/status.py","Status"], 
    ["/task.py","Task"], 
    ["/report.py","Report"] 
] 

static_html='<table id="main_nav" align="center"><tr>' 

for each_item in items: 
    if each_item in items: 
     if isinstance(each_item, list): 
      static_html= static_html + '<td><a href=" ', each_item[0], ' "> ', each_item[1], ' </a></th> ' 

static_html+='</tr></table>' 

print static_html 

屆時,IDE給我一個錯誤/usr/bin/python2.7

/home/tsuibin/code/aps/dxx/test_tmp.py 
Traceback (most recent call last): 
    File "/home/tsuibin/code/aps/dxx/test_tmp.py", line 39, in <module> 
    printMainNavigation() 
    File "/home/tsuibin/code/aps/dxx/test_tmp.py", line 20, in printMainNavigation 
    static_html= static_html + '<td><a href=" ', each_item[0], ' "> ', each_item[1], ' </a></th> ' 
TypeError: can only concatenate tuple (not "str") to tuple 
Process finished with exit code 1 
+1

有人格式化你的代碼,而現在你又搞砸了:)(現在的罰款) –

回答

5

你把逗號在字符串連接:

static_html= static_html + '<td><a href=" ', each_item[0], ' "> ', each_item[1], ' </a></th> ' 

,因此Python看到這些元素作爲一個元組(str0 + (str1, str2, str3))。使用+代替:

static_html= static_html + '<td><a href=" ' + each_item[0] + ' "> ' + each_item[1] + ' </a></th> ' 

更重要的是,使用字符串格式化:

static_html += '<td><a href="{0[0]}"> {0[1]} </a></th> '.format(each_item) 

當串聯一系列Python字符串的,它實際上將是更快地使用一個list()作爲中介。構建列表,然後使用''.join()得到輸出:

static_html = ['<table id="main_nav" align="center"><tr>'] 
for each_item in items: 
    static_html.append('<td><a href="{0[0]}"> {0[1]} </a></th> '.format(each_item)) 

static_html.append('</tr></table>') 
static_html = ''.join(static_html) 

請注意,您不需要測試each_item in items可言,你只是在迴路中已經得到了清單的項目。這只是額外的工作,沒有做任何事情。

+1

+1附加格式化例如 –

+0

非常感謝你! – tsuibin

4

其他人指出了錯誤信息的原因。我冒昧地重新編寫代碼。我所做的主要事情是避免字符串連接 - 在Python中,字符串是不可變的,所以連接需要創建一個完整的新字符串。避免這種情況的常用方法是將字符串的片段放入列表中,然後使用字符串的方法將列表中的所有元素連接在一起。

另一個主要變化是使用string.format()方法創建片段。

items = [ 
    ["/ank/homepage.py","Home"], 
    ["/ank/package.py","Packages"], 
    ["/status.py","Status"], 
    ["/task.py","Task"], 
    ["/report.py","Report"] 
] 

# Start a list of fragments with the start of the table. 
html_fragments = [ 
    '<table id="main_nav" align="center"><tr>' 
] 

for item in items: 
    # No need for the 'if item in items' here - we are iterating 
    # over the list, so we know its in the list. 
    # 
    # Also, this ifinstance() test is only required if you cannot 
    # guarantee the format of the input. I have changed it to allow 
    # tuples as well as lists. 
    if isinstance(item, (list, tuple)): 
     # Use string formatting to create the row, and add it to 
     # the list of fragments. 
     html_fragments.append('<td><a href="{0:s}">{1:s}</a></td>'.format(*item)) 

# Finish the table. 
html_fragments.append ('</tr></table>') 

# Join the fragments to create the final string. Each fragment 
# will be separated by a newline in this case. If you wanted it 
# all on one line, change it to ''.join(html_fragments). 
print '\n'.join(html_fragments) 

而且我得到的輸出:

<table id="main_nav" align="center"><tr> 
<td><a href="/ank/homepage.py">Home</a></td> 
<td><a href="/ank/package.py">Packages</a></td> 
<td><a href="/status.py">Status</a></td> 
<td><a href="/task.py">Task</a></td> 
<td><a href="/report.py">Report</a></td> 
</tr></table> 
+0

非常感謝你! – tsuibin