2014-02-10 40 views
1

我正在努力編寫下面的python腳本。 我有一個csv文件看起來像這樣使用逗號循環和 - 作爲分隔符python

"SNo","Title1","Title2" 
"A1-A3,A4-A5","A,B","C" 
"A6-A7","X","Y" 
"A8","Z","D" 

輸出應該是應該產生

"SNo","Title1","Title2" 
"A1","A,B","C" 
"A2","A,B","C" 
"A3","A,B","C" 
"A4","A,B","C" 
"A5","A,B","C" 
"A6","X","Y" 
"A7","X","Y" 
"A8","Z","D" 

我讀文件

cols= [0,1,2] 
with open('C:\\down\\1\\list.csv','rb') as f: 
    reader = csv.reader(f) 
    for row in reader: 
     content = list(row[i] for i in cols) 
     numberlist = content[0].replace("A","").split(",") 
     print numberlist[0],content[1],content[2] 

但我奮力csv文件除此之外。 請告訴我一個很好的方法來解決這個問題。

感謝

回答

2

你可以試試這個:

output_arr = [] 
for row in reader: 
    # Get the mapping for A ranges 
    idx_map = row[0].replace("A","").split(",") 
    for row_map in idx_map: 
      # For each pair or #-#'s creating the min and max idxs 
      mapping = [int(v) for v in row_map.split('-')] 
      min_map = min(mapping) 
      max_map = max(mapping) 
      for idx in range(min_map,max_map+1): 
        # For each value in range min_map to max_map, set values of row. 
        output_arr.append(["A%i"%(idx),row[1],row[2]]) 

>>> import pprint 
>>> pprint.pprint(output_arr) 
[['A1', 'A,B', 'C'], 
['A2', 'A,B', 'C'], 
['A3', 'A,B', 'C'], 
['A4', 'A,B', 'C'], 
['A5', 'A,B', 'C'], 
['A6', 'X', 'Y'], 
['A7', 'X', 'Y'], 
['A8', 'Z', 'D']] 

這將處理異常情況,如:「A3-A1」

0

我想這可能是一個起點:

with open('list.csv','rb') as f, open('res.csv', 'wb') as f2: 
    reader = csv.reader(f) 
    writer = csv.writer(f2) 
    for row in reader: 
     for group in row[0].split(','): 
      limits = group.split('-') 
      if len(limits) == 2: 
       id1, id2 = [int(x[1:]) for x in limits] 
       for num in range(id1, id2+1): 
        writer.writerow(("A{}".format(num),row[1],row[2])) 
      else: 
       writer.writerow((group,row[1],row[2])) 

你需要,如果你想你張貼的確切輸出格式調整它一點點。

1

假設data是從csv文件來保存數據列表的列表:

data = [["A1-A3,A4-A5","A,B","C"], 
     ["A6-A7","X","Y"], 
     ["A8","Z","D"]] 

for line in data: 
    head, tail = line[0], line[1:] 
    for range_ in head.split(","): 
     try: 
      from_, to = range_.split("-") 
      c, n, m = from_[0], int(from_[1:]), int(to[1:]) 
      for i in range(n, m+1): 
       print c + str(i), tail 
     except: 
      print range_, tail 

輸出:

A1 ['A,B', 'C'] 
A2 ['A,B', 'C'] 
A3 ['A,B', 'C'] 
A4 ['A,B', 'C'] 
A5 ['A,B', 'C'] 
A6 ['X', 'Y'] 
A7 ['X', 'Y'] 
A8 ['Z', 'D'] 
相關問題