我使用Python将SQLite3数据库中的数据附加到数组中。现在我有一个数组,我试图以下列格式打印附加数据:
print "Team: " + new_array[0][0] + " Score: " + new_array[0][1] + " Overall: " + new_array[0][2]
我已经有了这个代码。但是,我正在尝试使用计数器创建一个循环,该计数器将以上述格式打印数组中的所有数据。
问题在于,当我尝试创建一个for循环并打印数组中的第一个值时,只打印该值的第一个字母。例如:
print new_array[0]
...会打印:
team_6
team_3
team_5
team_1
team_2
team_4
......和......
print new_array[0][0]
...会打印:
t
t
t
t
t
t
这是我到目前为止所做的: import sqlite3
new_array = []
conn = sqlite3.connect('league.db')
c = conn.cursor()
game = c.execute("SELECT Team,Score,Overall FROM League ORDER BY Overall DESC")
for items in game:
new_array.append(items)
print new_array[0][0]
上面的代码会打印出'team_6'。但是,打印't'的问题只发生在我创建for循环时。
非常感谢您的帮助。感谢。
答案 0 :(得分:1)
尝试此操作以获得预期的输出
for items in game.fetchall():
print items[0] # should be `team6` and so on
如果你想放入array.Do喜欢这个
for items in game.fetchall():
new_array.append(items)
>>>new_array[0][0]
'team6'
答案 1 :(得分:1)
这是由您循环的值引起的。您认为new_array[0][0]
是一个单词,但实际上new_array[0]
是单词,[0][0]
是该单词中的第一个字符。因此,如果您想打印单词而不是仅打印第一个字母,请使用new_array[0]
答案 2 :(得分:1)
你制作阵列的方式已成为一个字符串。像以下
一样使用append()
a = list();
a.append(items); # these will be your string elements
print(a[0]) # will print your first item