I have a csv file that holds information that I am feeding to a python script. The list stored in the csv looks similar to ['1','2','3','4','5']
. When I create a loop to print out the contents of the list, I get:
'(new line)
1(new line)
'(new line)
'(new line)
2(new line)
'(new line)
'(new line)
3(new line)
'(new line)
.. until it reaches the end. How can I extract the numerical contents of the list without the parenthesis and brackets? I tried the .replace()
but when I have numbers higher than 10, it prints out 1 then 0 as if they were two separate values.
CodePudding user response:
It seems like you list is not stored in proper .csv format. When saving a list of items in a csv file, each of the items should be separated by a single comma. Any other characters will be considered part of the item itself.
In your case when you save ['1','2','3','4','5']
as a csv, the first item becomes ['1'
the second item becomes '2'
and so on. Try saving the text 1,2,3,4,5
to your csv file.
CodePudding user response:
Provided you have a list something like this as a list from the CSV
file:
list_example = ["1","2","3","4"]
And, you would like to retrieve the items of the list as an integer, you could do the following:
for i in list_example:
print(int(i))
This will give you the result as below in integer "int"
type:
>>1
>>2
>>3