如何将输出中的 3 行信息格式化在一起而不是分开?

How do format 3 lines of information in the output together instead of them being separate?

我正在尝试输出两行代码而不是两行单独的代码。

输出是 Twitter 用户名和他们转推的次数,使用 excel sheet 模块 xlrl 的完整数据。

for cell in sheet.col(23):
    print("username:",cell.value)

for cell in sheet.col(11):
    print("has:",cell.value, "retweets")

for cell in sheet.col(19):
    print("and mentioned:",cell.value)    

print(cell.value)

我期望输出:

username: NicoletteColl20
has: 8.0 retweet
and mentioned: GhostKumi

然而输出是:

-username: NicoletteColl20
-username: STEM_Portsmouth
-has: 8.0 retweets
-has: 188.0 retweets
-and mentioned: mentions
-and mentioned: GhostKumi

使用zip同时遍历列,"side by side"(假设每列的元素个数相同,否则较长列中的某些值将不会被打印出来) :

for c1, c2, c3 in zip(sheet.col(23), sheet.col(11), sheet.col(19)):
    print("username:",c1.value) 
    print("has:",c2.value, "retweets")
    print("and mentioned:",c3.value)

可能您选择的列有 2 行,您也需要遍历这些行。

一个选项是删除不需要的行:

sheet: [A,B,用户名,...,转推,...,提及,...,C,D]

我的sheet:[用户名、转推、提及]

然后做这样的事情:

for r in mysheet.row:
    print("username:",r[0])
    print("has:",r[1], "retweets")
    print("and mentioned:",r[2])