Python、CSV 和 Excel:创建并排显示信息的文本文件

Python, CSV, and Excel: Creating a text file with info side by side

我想让这段代码创建一个文本文件,其中的信息并排显示为列,但现在它显示为行。

如果你想要完整的代码,这是我的 pastebin:

http://pastebin.com/TeD78wQn

with open('file.txt','wb') as fou:
    writer = csv.writer(fou)
    for row in data:
        writer.writerow(row)

我想这就是你想要的:

transposed = zip(*data)
with open('col_output.txt','wb') as fou:
    writer = csv.writer(fou)
    for row in transposed:
        writer.writerow(row)

编辑:要从原始数据中删除空格,只需在填充 data:

时调用 .strip()
data = []
data.append([sheet.cell_value(row, 0).strip() for row in range(sheet.nrows)])
data.append([sheet.cell_value(row, 1).strip() for row in range(sheet.nrows)])