从csv提取特定列 [英] Extract a particular column from a csv

查看:360
本文介绍了从csv提取特定列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.csv文件格式如下:

I have a .csv file in the following format:

ID1    ID2    city       Country
AR     xyz    Tokyo      Japan
AR            New York   USA
AR     abc    Vienna     Austria

第三列数据使用正则表达式,因此输出将是:

I want to extract the third column data from the file using a regular expression so the output would be:

Tokyo
New York
Vienna


推荐答案

http://docs.python.org/3/library/csv.htmlrel =nofollow> csv 模块?这可以更快。

Why not just use csv module? This can be much faster.

import csv

with open('this.file') as this_file:
  source = csv.reader(this_file, delimiter=' ')
  next(source)  # skipping header
  for row in source:
    print(row[3])

这篇关于从csv提取特定列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆