gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 02:31:57 25 4
gpt4 key购买 nike

我有体育比赛列表:

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,但我不知道如何将每个 [date match odd_1 odd_2] 数据与其特定的 [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 个部分,您设置 "sport, country, competition" 字段 - 如果它有 4 个元素,您用数据写入行,在最后一个之前“运动、国家、竞争”部分:

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() 来清理你的数据......

关于python - 创建 csv 文件,其中每一行都是一个列表中的一些嵌套列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63756743/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com