如何使用jupyter转置csv文件?

时间:2018-06-29 02:38:11

标签: python csv jupyter transpose

免责声明,我是编码的初学者,因此请放轻松,谢谢。

在jupyter中使用python将数据保存到csv文件之前,如何转置数据?

这是我的代码:

/etc/logstash/

1 个答案:

答案 0 :(得分:-1)

您正在错误地构建数据对象。 CSV是数据行,因此最好的选择是创建一个行数组,然后将其写出。像这样:

import csv

# First row is the title row
rows = [(
    "Product Name",
    "Product Image Is Thumbnail - 1",
    "Product Code/SKU",
    "Product Description",
    "Price",
    "Cost Price",
    "Product Image File - 1",
    "Product Image File - 2"
)]

csvFile = csv.reader(open("wedding.csv"))
for row in csvFile:
    # add a row of data transposing positions as desired
    # NOTE: the double parenthesis are intentional, we're appending a tuple to the array  
    rows.append((row[2], row[6], row[4], row[11], row[7], row[8], row[9], row[10]))

print(rows)

with open('test.csv', 'w') as csv_file1:
    writer = csv.writer(csv_file1, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL)
    for row in rows:
        # write out each row
        writer.writerow(row)
相关问题