0

I am working on a python script that reads user input and returns values from the CSV. I am able to return all values, but I only need a few. There are many columns in the CSV, examples are:

LOC_NBR LOC_NAME ALPHA_CODE FRANCHISE_TYPE FRANCHISEE_LAST_NAME

My code is below, what could I add to this to only pull the data for say LOC_NBR, LOC_NAME, and FRANCHISE_TYPE? Right now if I change the print statement, I get a data type error because the fields are STR in the csv.

import csv

store_Num = input("Enter 5-Digit Store Number: ")
    
with open('StoreDirectory.csv', newline='') as csvfile:
    reader = csv.reader(csvfile)

    found = False
    
    for line in reader:
            if line[0] == store_Num:
                    print(line)
                    found = True
                    break
    if not found:
            print(store_Num, "not found")
4
  • 1) Does the CSV file have the field names as a header? 2) From here csv look at csv.DictReader(). If the file has a header then you do something like line["LOC_NBR"]. Commented Jul 28, 2022 at 18:34
  • I believe it has headers, but I'm not 100% sure how to check. There are names in the first row of the CSV when opening in excel. I made the suggested change and got Enter 5-Digit Store Number: 10009 Traceback (most recent call last): File "xxxx", line 12, in <module> if line[0] == store_Num: KeyError: 0 Commented Jul 28, 2022 at 19:33
  • I think I might have gotten this to work, will update after testing Commented Jul 28, 2022 at 19:45
  • This is now working. Thank you so much for the idea. I hace to change if line["LOC_NBR"] == store_Num and print (line["LOC_NBR']....etc Commented Jul 28, 2022 at 20:01

2 Answers 2

1

Using Python csv:

cat csv_test.csv 
first,second
1, 1
3, 4

import csv

with open("csv_test.csv") as csv_file:
    c = csv.DictReader(csv_file)
    for row in c:
        if int(row["first"]) == 3:
            print(row["first"], row["second"])

3  4
Sign up to request clarification or add additional context in comments.

Comments

0

The fastest (or most convenient) way to do this might be to use the pandas module and import the data into a dataframe.

import pandas as pd

df = pd.read_csv('data.csv')

from here you can extract rows or columns as you like.

column = "column_name"
row = 2
print ( df[column][row] )

Ideally the dataframe needs column headers which will make life easy.

5 Comments

So you import a large module that creates a 'fat' object(df) to do the same thing that csv.DictReader() does?
for simplicity and convenience, yes. The entire data is read into the dataframe in one go, even if it is small data, so no need to readlines...
I've tried to use import pandas, but I can never get it to run correctly. I'm assuming something needs to be installed? I'm on a work PC so maybe not possible?
all that is required: pip install pandas. . . . . Both pandas and numpy are the first two modules that i install when using python: pandas.pydata.org/docs/getting_started/install.html
@SpencerGray, you don't need to install pandas to do what you want. You can make it work without using pandas using the the supplied global module csv. One less layer of complexity.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.