尝试使用python

时间:2016-07-25 02:00:57

标签: python-2.7 pandas

我正在尝试将CS​​V文件内容从格式A转换为格式B.我尝试过pandas,默认dict,Dict writer等,但我无法解决它。问题是它是水平打印而不是垂直打印。请查看下面的示例。enter image description here

格式A:

项目测量COL A COL B COL C COL D

84P37W265B3 B1 3970 99.82368 99.82368 0.07556675

84P37W265B3 B3 3960 95.10101

84P37W265B3 B5 3705 96.89609 96.89609 0.05398111

84P37W265B3 B6 3763 98.45868 98.45868 0.02657454

84P3XT135A4 B1 7904 99.73431 99.73431 0.02

84P3XT135A4 B3 7817 97.5694 100 0.01

格式B:

项目84P37W265B3 84P3XT135A4

meas B1 B1

COL A 3970 7904

COL B 99.82368 99.73431

COL C 99.82368 99.73431

COL D 0.07556675 0.02

测量B3 B3

COL A 3960 7817

COL B 95.10101 97.5694

COL C - 100

COL D - 0.01

meas B5 -

COL A 3705 -

COL B 96.89609 -

COL C 96.89609 -

COL D 0.05398111 -

meas B6 -

COL A 3763 -

COL B 98.45868 -

COL C 98.45868 -

COL D 0.02657454 -

任何人都可以帮助我,提前谢谢......

2 个答案:

答案 0 :(得分:0)

你可以使用python csv

  

这是将格式A转换为格式B的工作代码

import csv

a_csv = r'D:\A.csv'
b_csv = r'D:\B.csv'

#  Read Format A CSV Format
with open(a_csv) as csvfile:
    reader = csv.DictReader(csvfile)
    item_list = []
    item_list.insert(0, 'Item')
    meas_list = []
    meas_list.insert(0, 'meas')
    cola_list = []
    cola_list.insert(0, 'COL A')
    colb_list = []
    colb_list.insert(0, 'COL B')
    colc_list = []
    colc_list.insert(0, 'COL C')
    cold_list = []
    cold_list.insert(0, 'COL D')
    for row in reader:
        item_list.append(row['item'])
        meas_list.append(row['meas'])
        cola_list.append(row['COL A'])
        colb_list.append(row['COL B'])
        colc_list.append(row['COL C'])
        cold_list.append(row['COL D'])

#  Write Format B CSV Format
with open(b_csv, 'w') as csvfile:
    writer = csv.writer(csvfile)
    writer.writerow(item_list)
    writer.writerow(meas_list)
    writer.writerow(cola_list)
    writer.writerow(colb_list)
    writer.writerow(colc_list)
    writer.writerow(cold_list)

检查输出:

Item    84P37W265B3 84P37W265B3 84P37W265B3 84P37W265B3 84P3XT135A4

meas    B1  B3  B5  B6  B3 

COL A   3970    3960    3705    3763    7817

COL B   99.82368    95.10101    96.89609    98.45868    97.5694

COL C   99.82368        96.89609    98.45868    100

COL D   0.07556675      0.05398111  0.02657454  0.01

答案 1 :(得分:0)

我要感谢@cyclops的回复。请找到我的动态类型代码.i.e。用户不知道输入csv文件中的列数。

CODE:

import csv
from collections import defaultdict

column_header=[]

columns = defaultdict(list) 
with open('C:\outfile4.csv') as f:
  reader = csv.DictReader(f) 
  for row in reader: 
        for (k,v) in row.items(): 
            columns[k].append(v)


b_csv = r'C:\outfile5.csv'

with open(b_csv, 'w') as csvfile:
  writer = csv.writer(csvfile, delimiter=',', quotechar='|',   quoting=csv.QUOTE_MINIMAL)

    for key,values in sorted(columns.iteritems()):
      if key == 'item':
         writer.writerow([key]+values)
    for key,values in sorted(columns.iteritems()):
      if key == 'meas':
         writer.writerow([key]+values)
    for key,values in sorted(columns.iteritems()):
      if key != 'item' and key != 'meas':
         writer.writerow([key]+values)

它正在运作,但请让我做任何其他简单的方法。提前谢谢。