How to transpose a dataset in a csv file?

后端 未结 7 1035
太阳男子
太阳男子 2020-11-30 01:32

For example, i would like to transform:

Name,Time,Score
Dan,68,20
Suse,42,40
Tracy,50,38

Into:

Name,Dan,Suse,Tracy
Time,68,         


        
7条回答
  •  鱼传尺愫
    2020-11-30 02:06

    If lines is the list of your original text than it should be

    for i in range(1,len(lines)):
        lines[i] = lines[i].split(',')
    
    new_lines = []
    for i in range(len(lines[0])):
        new_lines.append("%s,%s,%s" % (lines[0][i], lines[1][i], lines[2][i]))
    

    or use csv Python module - http://docs.python.org/library/csv.html

提交回复
热议问题