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

Python CSV模块使用:新手如何提取指定标签内容?

提取CSV文件中的标签内容

你可以用Python的csv模块直接定位到tag.列提取内容,这里给你两种适合初学者的简单实现方式:

方式一:用DictReader(推荐,更直观)

DictReader会把每行数据转成字典,表头就是字典的键,直接通过列名就能获取对应内容:

import csv

# 替换成你的CSV文件路径
with open('your_csv_file.csv', 'r', newline='', encoding='utf-8') as file:
    csv_reader = csv.DictReader(file)
    # 遍历每一行,提取tag.列的内容
    for row in csv_reader:
        tag_content = row['tag.']
        print(tag_content)

方式二:用Reader(基于索引定位)

如果想理解列的索引逻辑,可以用这种方式,先获取表头找到tag.列的索引,再提取对应内容:

import csv

with open('your_csv_file.csv', 'r', newline='', encoding='utf-8') as file:
    csv_reader = csv.reader(file)
    # 读取表头行,找到tag.列的索引
    headers = next(csv_reader)
    tag_column_index = headers.index('tag.')
    # 遍历后续行,提取对应索引的内容
    for row in csv_reader:
        tag_content = row[tag_column_index]
        print(tag_content)

运行代码后,会输出:

cool,fun
Cell,phone
first,third

如果需要把每个标签拆分成单独元素(比如把cool,fun拆成['cool', 'fun']),可以在提取后加一行拆分代码:

tag_list = tag_content.split(',')
print(tag_list)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 03:21:01