为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕尼黑8549860 2023-07-18 10:17:39
我有体育比赛的名单: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)
查看完整描述

3 回答

?
DIEA

TA贡献1820条经验 获得超2个赞

您可以迭代列表,如果迭代的元素有 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 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()来清理您的数据......


查看完整回答
反对 回复 2023-07-18
?
四季花海

TA贡献1811条经验 获得超5个赞

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.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


查看完整回答
反对 回复 2023-07-18
?
MM们

TA贡献1886条经验 获得超2个赞

您可以根据 收集数据sports。数据可以放入字典中,按 分组columns。然后使用 pandas 将字典导出到 csv 中。


import pandas as pd


def func(table: list, columns=None, sports=None, output_name='file') -> None:

    """

    Take in a table of data from <<<where is this data coming from>>>

    :param table: a table of data from <<<>>>

    :param columns: give the column names of the data

    :param sports: provide all of the sports being searched for

    :param output_name: the name of the csv file being output

    :return: Nothing, a file is created

    """

    if sports is None:

        sports = ['Volleyball', 'Handball', 'American Football', 'Soccer', 'Tennis']  # tell the program what a sport is

    if columns is None:

        columns = ['sport', 'country', 'competition', 'date', 'match', 'odd_1', 'odd_2']  # make headers one list


    current_info = list()  # track the current sport line


    # collect the data

    data = []

    for i in range(len(table)):

        if table[i][0] in sports:

            current_info = table[i]

        else:

            data.append(current_info + table[i])


    # write data to dictionary to be passed to pd DataFrame

    dn = {columns[i]: [] for i in range(len(columns))}

    for i in range(len(data)):

        for j in range(len(data[i])):

            dn[columns[j]].append(data[i][j])


    pd.DataFrame(dn).to_csv(output_name + '.csv')  # export dataframe to csv


    return None

通过键入来调用它


func(table)


查看完整回答
反对 回复 2023-07-18
  • 3 回答
  • 0 关注
  • 165 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信