如何为…输入连接字符串。。。在里面(python)

如何为…输入连接字符串。。。在里面(python),python,string,Python,String,我是一个c程序员,但python是不同的,我遇到了一个问题 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"

我是一个c程序员,但python是不同的,我遇到了一个问题

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
项目=[
[“/ank/homepage.py”,“Home”],
[“/ank/package.py”,“Packages”],
[“/status.py”,“status”],
[“/task.py”,“task”],
[“/report.py”,“report”]
]
静态_html=“”
对于项目中的每个_项目:
如果项目中的每个项目:
如果存在(每个项目,列表):
静态html=静态html+“”
静态_html+=“”
打印静态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
/home/tsuibin/code/aps/dxx/test\u tmp.py
回溯(最近一次呼叫最后一次):
文件“/home/tsuibin/code/aps/dxx/test_tmp.py”,第39行,在
printMainNavigation()
文件“/home/tsuibin/code/aps/dxx/test_tmp.py”,第20行,在printMainNavigation中
静态html=静态html+“”
TypeError:只能将元组(而不是“str”)连接到元组
进程已完成,退出代码为1

您在字符串连接中添加了逗号:

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)

请注意,您根本不需要测试items中的每个项目,您已经从循环中的列表中获得了该项目。这只是额外的工作,没有任何作用。

您正在字符串串联中添加逗号:

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)

请注意,您根本不需要测试items中的每个项目,您已经从循环中的列表中获得了该项目。这只是额外的工作,没有任何作用。

其他人已经指出了错误消息的原因。我冒昧地重新编写了一些代码。我所做的主要工作是避免字符串连接——在Python中,字符串是不可变的,因此连接需要创建一个完整的新字符串。避免这种情况的一种常见方法是将字符串的片段放入列表中,然后使用字符串的方法将列表中的所有元素连接在一起

另一个主要变化是使用该方法创建片段

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)
项目=[
[“/ank/homepage.py”,“Home”],
[“/ank/package.py”,“Packages”],
[“/status.py”,“status”],
[“/task.py”,“task”],
[“/report.py”,“report”]
]
#以表的开头开始一个片段列表。
html_片段=[
''
]
对于项目中的项目:
#这里不需要'if item in items',我们正在迭代
#在名单上,所以我们知道它在名单上。
#
#此外,仅当您无法执行此ifinstance()测试时,才需要此ifinstance()测试
#保证输入的格式。我把它改成允许
#元组以及列表。
如果isinstance(项,(列表,元组)):
#使用字符串格式创建行,并将其添加到
#片段列表。
html_fragments.append(“”.format(*item))
#把桌子吃完。
html_fragments.append(“”)
#连接片段以创建最终字符串。每个片段
#在这种情况下,将由换行符分隔。如果你想要的话
#全部放在一行上,将其更改为“”。加入(html_片段)。
打印“\n”。加入(html\u片段)
我得到的结果是:

<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>

其他人已经指出了错误消息的原因。我冒昧地重新编写了一些代码。我所做的主要工作是避免字符串连接——在Python中,字符串是不可变的,因此连接需要创建一个完整的新字符串。避免这种情况的一种常见方法是将字符串的片段放入列表中,然后使用字符串的方法将列表中的所有元素连接在一起

另一个主要变化是使用该方法创建片段

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)
项目=[
[“/ank/homepage.py”,“Home”],
[“/ank/package.py”,“Packages”],
[“/status.py”,“status”],
[“/task.py”,“task”],
[“/report.py”,“report”]
]
#以表的开头开始一个片段列表。
html_片段=[
''
]
对于项目中的项目:
#这里不需要'if item in items',我们正在迭代
#在名单上,所以我们知道它在名单上。
#
#此外,仅当您无法执行此ifinstance()测试时,才需要此ifinstance()测试
#保证输入的格式。我把它改成允许
#元组以及列表。
如果isinstance(项,(列表,元组)):
#使用字符串格式创建行,并将其添加到
#片段列表。
html_fragments.append(“”.format(*item))
#把桌子吃完。
html_fragments.append(“”)
#连接片段以创建最终字符串。每个片段
#在这种情况下,将由换行符分隔。如果你想要的话
#全部放在一行上,将其更改为“”。加入(html_片段)。
打印“\n”。加入(html\u片段)
我得到的结果是:

<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>


有人格式化了你的代码,现在你又弄糟了:)(现在没事)有人格式化了你的代码,现在你又弄糟了:)(现在没事了)