Read csv file into list and convert the strings into integers Python
I am trying to read in a CSV file and then returns all the data into a list. I first tried to read in the csv file using
csv.reader by using the following code:
import csv with open(fileName, 'r') as f: next(f) data = csv.reader(f) dataList = list(data)
and I now have a list that looks like:
[['123', '234', '456', '567'] ['345', '3456', '5678', '678'] ['2345', '4567', '45678', '56789'] ...]
I noticed that the numbers are stored as strings in the list, so I created a new list by
newList = [int(i) for i in dataList] and I got this error message
TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'
so I tried the following code I saw on Youtube:
with open('fileName', 'r') as f: next(f) data = csv.reader(f) data_lst =  for line in reader: data_lst.append([int(line)])
and I get this error message:
ValueError Traceback (most recent call last) <ipython-input-123-9fbefdb892ab> in <module> 3 data = csv.reader(f) 4 data_lst =  ----> 5 for line in reader: 6 data_lst.append([int(line)]) ValueError: readline of closed file
Does anyone know how to convert the strings in the list into integers?
Sample csv file
number1 number2 number3 number4 0 123 456 567 5678 1 4567 3456 6789 2345 ....
I need to read this csv file and that store the data in a list so that each row in the csv file is a list like
[[123, 456, 567, 5678], [4567, 3456, 6789, 2345]...]
This is happening because python3 is reading and writing files in binary. So you can either convert bytes data in to string and continue, or use pandas to read the data which will mostly read your numbers as integers.
import pandas as pd df = pd.read_csv("filename") rows = df.values.tolist()
Pandas gives you more freedom to perform operations on rows. You can also use something like below to convert column's datatype into int
I am having some trouble trying to read a particular column in a csv file into a list in Python. Below is an example of my csv file: Col 1 Col 2 1,000,000 1 500,000 2 250,000 3 Basically I am wanting to add column 1 into a list as integer values and am having a lot of trouble doing so. I have tried: for row in csv.reader(csvfile): list = [int
The answer @brunoto provided is correct, I'd prefer this solution (closer to yours):
with open('fileName', 'r') as f: next(f) data = csv.reader(f) data_lst =  for line in reader: data_lst.append([int(x) for x in line])
Note that all that was needed was a change to the final line, looping over the elements of each line and turning them into
int individually, instead of trying to turn the whole line (a record, i.e. a
list) into a single
Python | Converting all strings in list to integers Interconversion between data types is facilitated by python libraries quite easily. But the problem of converting the entire list of string to integers is quite common in development domain.
It's because you have one list inside another one. Like you told us:
dataList = [['123', '234', '456', '567'],['345', '3456', '5678', '678'],['2345', '4567', '45678', '56789']]
In your code you're iterating over the inner lists, so you just need to iterate over the strings inside the inner lists. Like this:
int_list =  for inner_list in dataList: for string in inner_list: int_list.append(int(string))
or, with single line:
int_list = [int(string) for inner_list in dataList for string in inner_list]
That will have the output bellow:
[123, 234, 456, 567, 345, 3456, 5678, 678, 2345, 4567, 45678, 56789]
If you want a list of list of values, as the input, the code bellow will work.
outer_out_list =  for inner_list in dataList: innet_out_list =  for string in inner_list: innet_out_list.append(int(string)) outer_out_list.append(innet_out_list)
[[123, 234, 456, 567], [345, 3456, 5678, 678], [2345, 4567, 45678, 56789]]
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.
The open () is a built-in function for file handling in Python. Then we need CSV.reader () to get structured data from.csv files. data_CSV = csv.reader (file_CSV) A list is the most used and convenient data structure in python so converting CSV files data into a list makes the data manipulation easy.
While programming we may need to convert a string to list in Python. That could be for any other reason. But, a question arises here, how can we convert a string to different forms of lists? So, here in this tutorial, we are going to learn how we can convert a string into a list in Python.
The csv library contains objects and other code to read, write, and process data from and to CSV files. Reading CSV Files With csv. Reading from a CSV file is done using the reader object. The CSV file is opened as a text file with Python’s built-in open() function, which returns a file object.
- It seems the problem is that you have a nested list, do:
[[int(i) for i in l] for l in dataList]
- This is not actually true - using
open(), Python opens files in text mode, so reading is fine. The error of OP is actually in properly accessing the list.