创建 csv 文件,其中每一行是一个列表中的一些嵌套列表

我有体育比赛的名单:


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

列是:


sport  country  competition  date  match  odd_1  odd_2

前 3 列:[sport,  country,  competition]始终位于一个嵌套列表中,前面是一个或多个列列表[date,  match,  odd_1,  odd_2]


我想创建 csv,但我不知道如何将每个 [日期匹配 odd_1 odd_2] 数据与其特定的 [体育国家比赛] 相关联


我创建了这段代码:


with open(filename.csv, 'a', encoding='utf_8_sig') as csv_file: 

    w = csv.writer(csv_file, lineterminator='\n')

    header = 

    w.writerow(header)


    for row in table:

        w.writerow(row)


慕尼黑8549860
浏览 166回答 3
3回答

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

MM们

您可以根据 收集数据sports。数据可以放入字典中,按 分组columns。然后使用 pandas 将字典导出到 csv 中。import pandas as pddef func(table: list, columns=None, sports=None, output_name='file') -> None:&nbsp; &nbsp; """&nbsp; &nbsp; Take in a table of data from <<<where is this data coming from>>>&nbsp; &nbsp; :param table: a table of data from <<<>>>&nbsp; &nbsp; :param columns: give the column names of the data&nbsp; &nbsp; :param sports: provide all of the sports being searched for&nbsp; &nbsp; :param output_name: the name of the csv file being output&nbsp; &nbsp; :return: Nothing, a file is created&nbsp; &nbsp; """&nbsp; &nbsp; if sports is None:&nbsp; &nbsp; &nbsp; &nbsp; sports = ['Volleyball', 'Handball', 'American Football', 'Soccer', 'Tennis']&nbsp; # tell the program what a sport is&nbsp; &nbsp; if columns is None:&nbsp; &nbsp; &nbsp; &nbsp; columns = ['sport', 'country', 'competition', 'date', 'match', 'odd_1', 'odd_2']&nbsp; # make headers one list&nbsp; &nbsp; current_info = list()&nbsp; # track the current sport line&nbsp; &nbsp; # collect the data&nbsp; &nbsp; data = []&nbsp; &nbsp; for i in range(len(table)):&nbsp; &nbsp; &nbsp; &nbsp; if table[i][0] in sports:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; current_info = table[i]&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data.append(current_info + table[i])&nbsp; &nbsp; # write data to dictionary to be passed to pd DataFrame&nbsp; &nbsp; dn = {columns[i]: [] for i in range(len(columns))}&nbsp; &nbsp; for i in range(len(data)):&nbsp; &nbsp; &nbsp; &nbsp; for j in range(len(data[i])):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dn[columns[j]].append(data[i][j])&nbsp; &nbsp; pd.DataFrame(dn).to_csv(output_name + '.csv')&nbsp; # export dataframe to csv&nbsp; &nbsp; return None通过键入来调用它func(table)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python