Python读取CSV文件指定列内容的两种方法
本文介绍在Python中读取CSV文件指定列内容的两种实现方式。通过csv模块的DictReader按列名提取,或使用reader按索引位置提取。文中提供了基于Python 2.7的测试代码示例,展示了如何打开文件并遍历获取指定列数据,供开发者参考实现。
以下代码在macOS系统的Python 2.7环境下测试运行成功。
import csv
with open('/Users/wangzhao/Downloads/test.csv', 'U') as csvfile:
reader = csv.DictReader(csvfile)
column = [row['Employee Name'] for row in reader]
print column
import csv
with open('/Users/wangzhao/Downloads/test.csv', 'U') as csvfile:
reader = csv.reader(csvfile)
column = [row[2] for row in reader]
print column
以上关于Python读取CSV文件指定列内容的介绍到此结束,希望能为读者提供有益参考,欢迎继续支持本站获取更多技术分享。
免责声明:本文内容仅供参考,不代表本站观点。转载或引用请注明出处。









