Python函数用于读取.csv文件

时间:2014-10-21 13:03:16

标签: python csv file-io

我正在编写一个脚本,需要在文件名中附加一个唯一的ID。

文件名取自文本文件(第一个函数),传递给第二个函数,格式化它,然后传递给第三个函数,该函数应该搜索带有多列的.csv文件,找到正确的行(包含通过其他两个函数传入的值的行)并从列'FID'中的那一行获取值(作为int或字符串)。然后它应该打印这个值。

代码:

def get_file_name():
    # this func gets the name of the file to be renamed
    before_rename = open('C:/Users/my.path/before_rename.txt', 'r')
    to_be_renamed_unf = before_rename.readline()[1:]
    # remove the end CRs & LFs off of the string
    to_be_renamed = to_be_renamed_unf.strip()
    print("File name: " + to_be_renamed)
    return to_be_renamed

def get_fname():
    # get farmer name
    file_name = get_file_name()
    farmer_name = re.sub('[^A-Z]', ' ', file_name).rstrip().lstrip()
    print(farmer_name)
    return farmer_name

def get_id_from_file():
    # search csv for COOP & Name to find the FID
    csvfile = 'C:/Users/my.path/csv_file_to_read_from.csv'
    # create a dictionary from the csv
    csv_dict = csv.DictReader(open(csvfile))

    fname = get_fname()
    coop_name = 'CALMAN' 
    for row in csvfile:
        if fname and coop_name in row:  
            farmer_id = int(row['FID'])
            print(farmer_id)

get_id_from_file()

当前输出:

File name: unformatted_file_NAME 03928
NAME

所以它似乎完全跳过了搜索循环;因为这是前两个函数的预期输出,我没有错误。

部分.csv:

FID,Name,COOP
12345-29981662553784,bar FOO BAR, FOOBAR
12345-29981662553784,FOO BAR, FOOBAR
12345-29981662553784,FOO BAR, FOOBAR
12345-29981662553784,FOO BAR, FOOBAR
12345-29981662553784,FOO BAR, FOOBAR
12345-29981662553784,FOO BAR, FOOBAR
12345-29981662553784,FOO BAR, FOOBAR
12345-29981662553784,FOO BAR, FOOBAR
12345-29981662553784,FOO BAR, FOOBAR
12345-29981662553784,FOO BAR, FOOBAR
12345-29981662553784,FOO BAR, FOOBAR
12345-29981662553784,FOO BAR, FOOBAR
12345-29981662553784,FOO BAR, FOOBAR

2 个答案:

答案 0 :(得分:1)

试试这个:

#!/usr/bin/env python
#-*- coding:utf-8 -*-

import csv

def get_file_name():
    # this func gets the name of the file to be renamed
    before_rename = open('C:/Users/my.path/before_rename.txt', 'r')
    to_be_renamed_unf = before_rename.readline()[1:]
    # remove the end CRs & LFs off of the string
    to_be_renamed = to_be_renamed_unf.strip()
    print("File name: " + to_be_renamed)
    return to_be_renamed

def get_fname():
    # get farmer name
    file_name = get_file_name()
    farmer_name = re.sub('[^A-Z]', ' ', file_name).rstrip().lstrip()
    print(farmer_name)
    return farmer_name

def get_id_from_file():
    # search csv for COOP & Name to find the FID
    csvfile = 'C:/Users/my.path/csv_file_to_read_from.csv'
    # create a dictionary from the csv
    csv_dict = csv.DictReader(open(csvfile))

    fname = get_fname()
    coop_name = 'CALMAN' 
    for row in csv_dict:
        if fname in row:
            if coop_name in row:
                farmer_id = int(row['FID'])
                print(farmer_id)

get_id_from_file()

答案 1 :(得分:0)

您是否尝试过使用pandas.read_csv?使这个读取和搜索/过滤csv比你和我可以写的任何东西都更有效率。

http://pandas.pydata.org/pandas-docs/stable/generated/pandas.io.parsers.read_csv.html