0

我有一系列数字:

6577

我想在 .csv 文件中看到它们,如下所示:

6  
5  
7

我试过使用文件写入器,但它会在第一行写入所有数字。

序列是字符串..

s = ['6','5','7','7','6']
item_length = len(s)

with open('test.csv', 'wb') as test_file:
    file_writer = csv.writer(test_file)
    for i in range(item_length):
        file_writer.writerow([x[i] for x in s])
4

1 回答 1

2

如果您希望将每个列表项写成一行,您可以尝试:

s = ['6','5','7','7','6']
item_length = len(s)

with open('test.csv', 'wb') as test_file:
    file_writer = csv.writer(test_file)
    for item in s:
        file_writer.writerow(item)

此外,只需写出一列,这可能就足够了:

s = ['6','5','7','7','6']
with open('test.csv', 'wb') as test_file:
   test_file.write("\n".join(s) + "\n")
于 2012-12-20T11:48:59.033 回答