Kargoth
Kargoth

Reputation: 23

Python 3: How to read a csv file and store specific values as variables

I 'm new in SO, new at programming and even more with python haha,

I'm trying to read CSV files (which will contain different data types) and store specific values ("coordinates") as variables.

CSV file example (sorry for using code format, text didn't want to stay quiet):

$id,name,last_name,age,phone_number,addrstr,addrnum
1,Constance,Harm,37,555-1234,Ocean_view,1
2,Homer,Simpson,40,555-1235,Evergreen_Terrace,742
3,John,Doe,35,555-1236,Fake_Street,123
4,Moe,Tavern,20,7648-4377,Walnut_Street,126

I want to know if there is some easy way to store a specific value using the rows as index, for example: "take row 2 and store 2nd value in variable Name, 3rd value in variable Lastname" and the "row" for each storage will vary.

Not sure if this will help because my coding level is very crappy:

row = #this value will be taken from ANOTHER csv file
people = open('people.csv', 'r')
linepeople = csv.reader(people)
data = list(linepeople)
name = int(data[**row**][1])
lastname = int(data[**row**][2])
age = int(data[**row**][3])
phone = int(data[**row**][4])
addrstr = int(data[**row**][5])
addrnum = int(data[**row**][6])

I haven't found nothing very similar to guide me into a solution. (I have been reading about dictionaries, maybe that will help me?)

EDIT (please let me know if its not allowed to edit questions): Thanks for the solutions, I'm starting to understand the possibilities but let me give more info about my expected output:

I'm trying to create an "universal" function to get only one value at given row/col and to store that single value into a variable, not the whole row nor the whole column.

Example: Need to store the phone number of John Doe (column 5, row 4) into a variable so that when printing that variable the output will be: 555-1236

Upvotes: 2

Views: 18241

Answers (3)

Martin Evans
Martin Evans

Reputation: 46779

As the question has changed, a rather different approach would be needed. A simple get row/col type function would work but would be very inefficient. The file would need to be read in each time. A better approach would be to use a class. This would load the file in once and then you could get as many entries as you need. This can be done as follows:

import csv

class ContactDetails():
    def __init__(self, filename):
        with open(filename, "r") as f_input:
            csv_input = csv.reader(f_input)
            self.details = list(csv_input)

    def get_col_row(self, col, row):
        return self.details[row-1][col-1]

data = ContactDetails("input.csv")

phone_number = data.get_col_row(5, 4)
name = data.get_col_row(2,4)
last_name = data.get_col_row(3,4)

print "%s %s: %s" % (name, last_name, phone_number)

By using the class, the file is only read in once. This would print the following:

John Doe: 555-1236

Note, Python numbers indexes from 0, so your 5,4 has to be converted to 4,3 for Python.

Upvotes: 1

Martin Evans
Martin Evans

Reputation: 46779

You can use the CSV DictReader which will automatically assign dictionary names based on your CSV column names on a per row basis as follows:

import csv

with open("input.csv", "r") as f_input:
    csv_input = csv.DictReader(f_input)

    for row in csv_input:
        id = row['$id']
        name = row['name']
        last_name = row['last_name']
        age = row['age']
        phone_number = row['phone_number']
        addrstr = row['addrstr']
        addrnum = row['addrnum']

        print(id, name, last_name, age, phone_number, addrstr, addrnum)

This would print out your CSV entries as follows:

1 Constance Harm 37 555-1234 Ocean_view 1
2 Homer Simpson 40 555-1235 Evergreen_Terrace 742
3 John Doe 35 555-1236 Fake_Street 123
4 Moe Tavern 20 7648-4377 Walnut_Street 126

If you wanted a list of just the names, you could build them as follows:

with open("input.csv", "r") as f_input:
    csv_input = csv.DictReader(f_input)
    names = []

    for row in csv_input:
        names.append(row['name'])

    print(names)

Giving:

['Constance', 'Homer', 'John', 'Moe']

Upvotes: 2

agomcas
agomcas

Reputation: 705

You can iterate line by line. Watch out for your example code, you are trying to cast names of people into integers...

for row in linepeople:
    name=row['name']
    age = int(row['age'])

If you are going to do more complicated stuff, I recommend pandas. For starters it will try to convert numerical columns to float, and you can access them with attribute notation.

import pandas as pd
import numpy as np
people = pd.read_table('people.csv', sep=',')

people.name  #  all the names
people.loc[0:2] # first two rows

Upvotes: 2

Related Questions