In this tutorial, you will learn how to read CSV columns into a list in Python. I am giving the following examples:
- Read CSV Columns into list and print on the screen.
- Read and Print specific columns from the CSV using csv.reader method.
- Read CSV via csv.DictReader method and Print specific columns.
For the below examples, I am using the country.csv file, having the following data:
COUNTRY_ID,COUNTRY_NAME,REGION_ID AR,Argentina,2 AU,Australia,3 BE,Belgium,1 BR,Brazil,2 CA,Canada,2 CH,Switzerland,1 CN,China,3
1. Read CSV Columns into List and Print on The Screen
In the following Python program, it will read the CSV file using the csv.reader method of CSV module and will print the lines.
import csv with open("country.csv", "r") as csv_file: csv_reader = csv.reader(csv_file, delimiter=',') for lines in csv_reader: print(lines)
Output:
['COUNTRY_ID', 'COUNTRY_NAME', 'REGION_ID'] ['AR', 'Argentina', '2'] ['AU', 'Australia', '3'] ['BE', 'Belgium', '1'] ['BR', 'Brazil', '2'] ['CA', 'Canada', '2'] ['CH', 'Switzerland', '1'] ['CN', 'China', '3']
Note: To skip the first line (header row), you can use next(csv_reader) command before the FOR LOOP.
2. Read and Print Specific Columns from The CSV Using csv.reader Method
In the following example, it will print the column COUNTRY_NAME, by specifying the column number as 1 (lines[1]). The CSV file is containing three columns, and the column number starts with 0 when we read CSV using csv.reader method.
import csv with open("country.csv", "r") as csv_file: csv_reader = csv.reader(csv_file, delimiter=',') for lines in csv_reader: print(lines[1])
Output:
COUNTRY_NAME Argentina Australia Belgium Brazil Canada Switzerland China
3. Read CSV via csv.DictReader Method and Print Specific Columns
In the following example, it will read the CSV into a list using csv.DictReader method and will print the columns using column names (COUNTRY_ID, COUNTRY_NAME) available in the header.
import csv with open("country.csv", "r") as csv_file: csv_reader = csv.DictReader(csv_file, delimiter=',') for lines in csv_reader: print(lines['COUNTRY_ID'], lines['COUNTRY_NAME'])
Output:
AR Argentina AU Australia BE Belgium BR Brazil CA Canada CH Switzerland CN China
how can we change column values from string to float in csv file in python?
The above example is to read values from a CSV file and after reading a value into list you can convert it using the float() function.
If I am not getting it right, then describe you question in more detail on our forum orclqa.com
How to parse subcolumn in nested column?
Example:
import csv
with open("country.csv", "r") as csv_file:
csv_reader = csv.DictReader(csv_file, delimiter=',')
for lines in csv_reader:
print(lines['COUNTRY'], lines['CAPITAL'])
AR Argentina Buenos Aires
Country has data Country_ID and Country_Name.
I want to remove first subcolumn Country_ID.
How to achieve that?
I think you have to use the functions to pick the substring of a string. For example, if you know that the country id is of 2 character length, then you can use the slicing method as following:
Now the first 3 characters (country id and a space) will be removed from lines['COUNTRY'].
Is there any method?
because I don't know exactly the characters length of the first subcolumn.
It's only has delimiter (for example white space or tab)
You can use split() function.
Example:
it will split on a space and will limit to one split.
Great.
can you help me how to trim and save it to output file?
You need to follow the below steps to write to a file using Python:
Ok. thank you very much
how to get it in same line?
Put end = " " in the end of print command. Following are the examples:
or
For Python version 2.x, add comma in end:
How to select a specific column value and use it in a iteration?
Check the below example:
The variable v_column_value will be assigned a country name value if the variable n is 0.
And after assigning, it will set n to 1, so that in next iteration it will not change.
So now you have the value of a column in a variable, you can use it in whole iteration.
if n = 0:
^
SyntaxError: invalid syntax
still have the same problem, when I wrote my own code and when I pasted yours. do you have any idea why is that?
oh it was mistake in the code, it should be if n== 0: instead of if n=0:.
how to sum the paticular column in csv file
How to know how many lines on the csv file and how to print only for certain line, lets say print line 3
Thank you
How would I add a column or multiple column to separate lists using this format