You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何遍历嵌套列表并生成指定格式的CSV文件?

解决方法:生成指定格式的嵌套列表CSV

我明白你的问题了——你需要把嵌套列表转置后,还要把SampleSentences对应的子列表展开成多行,同时其他列在这些行里对应填充内容或者[]。原来的代码只是简单转置,没有处理嵌套的子列表,所以格式不对。下面是完整的解决方案:

完整代码实现

import csv

my_list = [
    ['first-column', 'DisplayName', 'FLOW TRIGGERED: 636e56d390c8c0910d592cc6', 'ClassificationType', 'NLU', 'KeyPhrases', 'MetaIntent', 'Description', 'test description', 'SampleSentences', [], 'Regexes'], 
    ['first-column', 'DisplayName', 'FLOW TRIGGERED: 636e56d390c8c0910d592cc6', 'ClassificationType', 'NLU', 'KeyPhrases', 'MetaIntent', 'Description', 'test description', 'SampleSentences', [], 'Regexes'], 
    ['first-column', 'DisplayName', 'FLOW TRIGGERED: 636e56d490c8c01802592cd1', 'ClassificationType', 'NLU', 'KeyPhrases', 'MetaIntent', 'Description', 'test description', 'SampleSentences', ['Pressemitteilung?\n', 'Pressemeldung?\n', 'Wo finde ich den Schlussbericht zur Messe?\n'], 'Regexes'], 
    ['first-column', 'DisplayName', 'FLOW TRIGGERED: 636e56d490c8c0edac592cd8', 'ClassificationType', 'NLU', 'KeyPhrases', 'MetaIntent', 'Description', 'test description', 'SampleSentences', ['Aussteller?\n', 'Ausstellerverzeichnis 2022?\n', 'Welche Aussteller waren 2022 dabei?\n', 'Ausstellerliste 2022?\n', 'Welche Unternehmen waren als Aussteller vertreten?\n'], 'Regexes'], 
    ['first-column', 'DisplayName', 'FLOW TRIGGERED: 636e56d490c8c01739592ce0', 'ClassificationType', 'NLU', 'KeyPhrases', 'MetaIntent', 'Description', 'test description', 'SampleSentences', ['Wie hoch war die Ausstellerzahl 2022?\n', 'Wie viele Unternehmen waren vor Ort\n', 'Anzahl Aussteller?\n', 'Ausstellerzahl?\n', 'Wie viele Aussteller waren auf der Messe vertreten?\n'], 'Regexes']
]

# 转置原列表,将行转换为列
transposed = list(zip(*my_list))

# 提取SampleSentences的内容列(原列表中第11个元素,索引为10)
sample_sentences_data = transposed[10]

# 计算最长的子列表长度,确定需要生成多少行来展开所有句子
max_sentence_rows = max(len(sublist) for sublist in sample_sentences_data)

with open('test.csv', "w", newline='') as f:
    # 设置换行符为\n\n,实现每行之间的空行分隔
    writer = csv.writer(f, lineterminator='\n\n')
    
    # 先写入前10行(从first-column到SampleSentences标题行)
    for row in transposed[:10]:
        writer.writerow(row)
    
    # 展开SampleSentences的子列表,逐行生成内容
    for i in range(max_sentence_rows):
        current_row = []
        for sublist in sample_sentences_data:
            if i < len(sublist):
                # 去掉句子末尾的换行符,保持格式整洁
                current_item = sublist[i].strip()
                current_row.append(current_item)
            else:
                # 没有对应句子时,填充[]
                current_row.append("[]")
        writer.writerow(current_row)
    
    # 最后写入Regexes行
    writer.writerow(transposed[11])

代码解释

  1. 转置列表:zip(*my_list)将原列表的行转换为列,这样我们可以把同一类型的内容(比如所有first-column)放到同一行。
  2. 定位嵌套数据:单独提取SampleSentences对应的子列表列,方便后续展开处理。
  3. 计算最大行数:找到最长的子列表长度,确保所有句子都能被展开到对应的行中。
  4. 写入基础行:先把转置后的前10列(从first-column到SampleSentences标题)直接写入CSV。
  5. 展开嵌套子列表:遍历每个行索引,为每个子列表提取对应位置的句子(去掉多余换行符),没有对应句子时填充[],生成每行内容后写入。
  6. 写入最后一行:把Regexes对应的列写入CSV,完成整个文件的生成。

这样生成的CSV就会完全符合你指定的格式了。

内容的提问来源于stack exchange,提问作者Jananath Banuka

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 16:56:14