Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading each column from csv file

Tags:

python

csv

I want to read each column of a csv file and do some modification before storing them into table.

I have a csv files as :

"1";"testOne";"ValueOne"
"2";"testTwo";"ValueTwo"
"3";"testThree";"ValueThree"

Here I want to read the first value "1" and then store it somewhere in a varaible and do something with this value, and similary with the others. However currently I can read the whole file, but could not find the way to access individual columns in a row.

Thank you.

like image 652
Rohita Khatiwada Avatar asked Apr 21 '11 08:04

Rohita Khatiwada


People also ask

How do I read a column in a CSV file?

Use pandas. read_csv() to read a specific column from a CSV file. To read a CSV file, call pd. read_csv(file_name, usecols=cols_list) with file_name as the name of the CSV file, delimiter as the delimiter, and cols_list as the list of specific columns to read from the CSV file.

How do I extract a column from a CSV file?

Steps. Make a list of columns that have to be extracted. Use read_csv() method to extract the csv file into data frame. Print the exracted data.


1 Answers

Python has a built-in csv module.

import csv
with open('some.csv', 'rb') as f:
    reader = csv.reader(f, delimiter=';')
    for row in reader:
        print row[0]
like image 151
kgiannakakis Avatar answered Sep 29 '22 03:09

kgiannakakis