将数据拆分为csv文件中的列

将数据拆分为csv文件中的列

本文介绍了Python-将数据拆分为csv文件中的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的csv文件中有数据,看起来像是这样导入的.

I have data in a csv file that looks like that is imported as this.

import csv

with open('Half-life.csv', 'r') as f:
    data = list(csv.reader(f))

数据将以此形式输出到打印出data[0] = ['10', '2', '2']等行的位置.

the data will come out as this to where it prints out the rows like data[0] = ['10', '2', '2'] and so on.

我想要的是将数据检索为列而不是行,在这种情况下,该数据为3列.

What i'm wanting though is to retrieve the data as columns in instead of rows, to where in this case, there are 3 columns.

推荐答案

您可以创建三个单独的列表,然后使用 csv.reader .

You can create three separate lists, and then append to each using csv.reader.

import csv

c1 = []
c2 = []
c3 = []
with open('Half-life.csv', 'r') as f:
    reader = csv.reader(f, delimiter=',')
    for row in reader:
        c1.append(row[0])
        c2.append(row[1])
        c3.append(row[2])

这篇关于Python-将数据拆分为csv文件中的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 07:14