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

问题描述:

我有一些体育比赛列表:

I have list of lists of sports matches:

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列: [运动,国家/地区,比赛] 始终位于一个嵌套列表中,其后是一个或多个列 [date,match,我想创建csv,但是我不知道如何关联,所以odd_1,odd_2]

First 3 columns : [sport, country, competition] are always in one nested list, preceded by one or many list of columns [date, match, odd_1, odd_2]

使用每个特定的[运动国家/地区比赛]的[日期匹配奇数_1奇数_2]数据

I want to create the csv but I don't know how to associate each [date match odd_1 odd_2] data with it's specific [sport country competition]

我创建了以下代码:

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)


如果列表中的元素有3个部分,则可以设置列表运动,国家/地区,比赛 字段-如果它包含4个元素,则将数据写入行中,并在最后的 运动,国家/地区,比赛 部分之前:

You iterate the list, if the iterated element has 3 parts you set the "sport, country, competition" fields - if it has 4 elements you write the row with the data, prepending the last "sport, country, competition" parts:

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 csv

with 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_2
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
American Football, USA,NCAA,Today 20:00,Army - Middle Tennessee St,2.01,1.99
American Football, USA,NCAA,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
Soccer, World,Club Friendly,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




您可能应该抛出一些 str.strip()来清理数据...