简体   繁体   中英

Python functions to read a .csv file

I am writing a script which needs to append a unique ID to a file name.

The file name is taken from a text file (the first function), passed to a second function which formats it, then passed to a third function which is supposed to search a .csv file with multiple columns, find the right row (the row which contains the values passed in through the other two functions) and get the value (as an int or a string) from that row which is in the column 'FID'. It should then print this value.

The code:

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()

And the current output:

File name: unformatted_file_NAME 03928
NAME

So it would seem that it is skipping the search loop entirely; as this is the expected output of the first two functions and I'm getting no errors.

Some of the .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

Try this:

#!/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()

Have you tried using pandas.read_csv? Makes this reading and searching/filtering of csv much more efficient than anything you and I could prob write.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM