Python 使用逗号和-作为分隔符的循环

Python 使用逗号和-作为分隔符的循环,python,csv,delimiter,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","

我正在努力编写以下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]
但我正在努力超越这一点。 请告诉我一个好方法


谢谢

我想这可能是一个起点:

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]))

如果您想要发布的确切输出格式,您需要对其进行一些调整。

假设
数据
是一个列表,其中包含来自
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']
你可以试试这个:

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”