用逗号循环和 - 作为分隔符python

时间:2014-02-10 21:55:07

标签: python csv delimiter

我正在努力编写以下python脚本。 我有一个csv文件,看起来像这样

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

输出应该是应生成

的csv文件
"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]

但我正在努力超越这个。 请告诉我一个很好的方法来解决这个问题。

由于

3 个答案:

答案 0 :(得分: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”

答案 1 :(得分: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']

答案 2 :(得分: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]))

如果你想要发布的确切输出格式,你需要稍微调整一下。