DIEA
您可以迭代列表,如果迭代的元素有 3 个部分,您可以设置字段"sport, country, competition"- 如果它有 4 个元素,您可以在行中写入数据,并在最后一个 "sport, country, competition"部分前面添加:table = [['Volleyball', ' Europe', 'European Championships'],['Today 17:00', 'Moldova - Cyprus', '2.00', '1.72'],['Handball', ' Slovenia', '1. NLB Liga'],['Today 17:00', 'Krka - Slovenj Gradec', '2.05', '1.98'],['American Football', ' USA', 'NCAA'],['Today 17:00', 'Marshall - Eastern Kentucky', '1.90', '1.90'],['Today 20:00', 'Army - Middle Tennessee St', '2.01', '1.99'],['Tomorrow 20:00', 'West Virginia - Florida State', '2.50', '1.50'],['Soccer', ' World', 'Club Friendly'],['Today 17:00', 'UE Sants (Esp) - CE Europa (Esp)', '1.84', '1.88'],['Today 17:00', 'Spain - France', '1.20', '2.80'],['Tennis', ' USA', 'ATP US Open'],['Today 17:30', 'Berrettini M. - Ruud C.', '1.81', '2.02']]import csvwith open("file.csv", "w", newline="") as f: writer = csv.writer(f) # write header writer.writerow( "sport country competition date match odd_1 odd_2".split()) # write data for inner_list in table: if len(inner_list) == 3: # decompose for clarity sake, could as well just store it in some # other list: remember_me = inner_list sport, country, competition = inner_list else: # and do writerow( remember_me + inner_list) here writer.writerow([sport, country, competition] + inner_list)with open("file.csv") as f: print(f.read())输出:sport,country,competition,date,match,odd_1,odd_2Volleyball, Europe,European Championships,Today 17:00,Moldova - Cyprus,2.00,1.72Handball, Slovenia,1. NLB Liga,Today 17:00,Krka - Slovenj Gradec,2.05,1.98American Football, USA,NCAA,Today 17:00,Marshall - Eastern Kentucky,1.90,1.90American Football, USA,NCAA,Today 20:00,Army - Middle Tennessee St,2.01,1.99American Football, USA,NCAA,Tomorrow 20:00,West Virginia - Florida State,2.50,1.50Soccer, World,Club Friendly,Today 17:00,UE Sants (Esp) - CE Europa (Esp),1.84,1.88Soccer, World,Club Friendly,Today 17:00,Spain - France,1.20,2.80Tennis, USA,ATP US Open,Today 17:30,Berrettini M. - Ruud C.,1.81,2.02您可能应该投入一些str.strip()来清理您的数据......
四季花海
groupby您可以使用from itertools解析嵌套的列表列表:import itertools as it# Python 3.7+ only because of ordered dict...di={}for k,v in it.groupby(table, key=lambda li: len(li)==3): if k: di[tuple(list(v)[0])]=[] else: di[list(di.keys())[-1]].extend(v) 或者,你可以这样做:di={}for k,v in it.groupby(table, key=lambda li: len(li)==3): if k: last_key=tuple(list(v)[0]) di[last_key]=[] else: di[last_key].extend(v)然后迭代字典:# probably use csv but as an example...for k, v in di.items(): for match in v: print(','.join(k), ','.join(match)) 印刷:Volleyball, Europe,European Championships Today 17:00,Moldova - Cyprus,2.00,1.72Handball, Slovenia,1. NLB Liga Today 17:00,Krka - Slovenj Gradec,2.05,1.98American Football, USA,NCAA Today 17:00,Marshall - Eastern Kentucky,1.90,1.90American Football, USA,NCAA Today 20:00,Army - Middle Tennessee St,2.01,1.99American Football, USA,NCAA Tomorrow 20:00,West Virginia - Florida State,2.50,1.50Soccer, World,Club Friendly Today 17:00,UE Sants (Esp) - CE Europa (Esp),1.84,1.88Soccer, World,Club Friendly Today 17:00,Spain - France,1.20,2.80Tennis, USA,ATP US Open Today 17:30,Berrettini M. - Ruud C.,1.81,2.02