我刚开始学习如何用python处理数据,将数据从列表转换为表。我想知道允许用户输入追加或删除表项的代码。每个条目应包括组织名称,成立年份和现任首席执行官。
下面是我的代码:
# import texttable module
import texttable as tt
# create texttable object
tableObj = tt.Texttable()
# create heading
headings = ["ORGANIZATION", "ESTABLISHED", "CEO"]
tableObj.header(headings)
# create list as row values
organization = ['Google', 'Microsoft', 'Nokia', 'Geeks for Geeks', 'HackerRank']
established = [1998, 1975, 1865, 2008, 2007]
ceo = ["Sundar Pichai","Satya Nadella", "Rajeev Suri", "Sandeep Jain", "Vivek Ravisankar"]
# adding list into row
for row in zip(organization, established, ceo):
tableObj.add_row(row)
# display table
print(tableObj.draw())
谢谢大家!我可能会在不同的线程中发布与此相关的其他几个问题。祝你有个愉快的一天,请原谅一个新手:)
使用append()
方法来附加一个项目,例如使用以下代码:
import texttable as table
def display(i, organization, established, ceo):
for k in i:
organization.append(k)
if k==i[1]:
established.append(k)
if k==i[2] :
ceo.append(k)
t = table.Texttable()
headings = ["organization", "established.", "ceo"]
t.header(headings)
#t.set_chars(["-"," ","+","~"])
t.set_deco(table.Texttable.BORDER)
for row in zip(organization, established, ceo):
t.add_row(row)
s = t.draw()
print(s + "n")
return organization, established, ceo
这运行:
`print(display(['Stack',2000,'Salio'],['Google', 'Microsoft', 'Nokia', 'Geeks for Geeks', 'HackerRank'],[1998, 1975, 1865, 2008, 2007],["Sundar Pichai","Satya Nadella", "Rajeev Suri", "Sandeep Jain", "Vivek Ravisankar"])
输出添加此列表['Stack',2000,'Salio']
+---------------------------------------------------+
| organization established. ceo |
| Google 1998 Sundar Pichai |
| Microsoft 1975 Satya Nadella |
| Nokia 1865 Rajeev Suri |
| Geeks for Geeks 2008 Sandeep Jain |
| HackerRank 2007 Vivek Ravisankar |
| Stack 2000 Salio |
+---------------------------------------------------+
同样可以通过"Theremove()
method remove The指定项"来删除。