使用 csv 模塊從 csv 文件中讀取特定列? - Read specific columns from a csv file with csv module?

問題:

I'm trying to parse through a csv file and extract the data from only specific columns.我正在嘗試解析 csv 文件並僅從特定列中提取數據。

Example csv:示例 csv:

ID | Name | Address | City | State | Zip | Phone | OPEID | IPEDS |
10 | C... | 130 W.. | Mo.. | AL... | 3.. | 334.. | 01023 | 10063 |

I'm trying to capture only specific columns, say ID , Name , Zip and Phone .我試圖只捕獲特定的列,比如IDNameZipPhone

Code I've looked at has led me to believe I can call the specific column by its corresponding number, so ie: Name would correspond to 2 and iterating through each row using row[2] would produce all the items in column 2. Only it doesn't.我看過的代碼讓我相信我可以通過相應的編號來調用特定的列,所以 IE: Name將對應於2並且使用row[2]遍歷每一行將產生row[2]列中的所有項目。它沒有。

Here's what I've done so far:這是我到目前爲止所做的:

import sys, argparse, csv
from settings import *

# command arguments
parser = argparse.ArgumentParser(description='csv to postgres',\
 fromfile_prefix_chars="@" )
parser.add_argument('file', help='csv file to import', action='store')
args = parser.parse_args()
csv_file = args.file

# open csv file
with open(csv_file, 'rb') as csvfile:

    # get number of columns
    for line in csvfile.readlines():
        array = line.split(',')
        first_item = array[0]

    num_columns = len(array)
    csvfile.seek(0)

    reader = csv.reader(csvfile, delimiter=' ')
        included_cols = [1, 2, 6, 7]

    for row in reader:
            content = list(row[i] for i in included_cols)
            print content

and I'm expecting that this will print out only the specific columns I want for each row except it doesn't, I get the last column only.我希望這將只打印出我想要的每一行的特定列,除非它沒有,我只得到最後一列。


解決方案:

參考一: https://en.stackoom.com/question/17FKS
參考二: https://stackoom.com/question/17FKS
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章