1

I am getting a number from a csv file to a list and would like to compare these numbers to other numbers, how can I achieve this?

num = [2,32,31,23,12,32]

csvnumber= ['23,43,41,21,34']

How do I convert the csv number into ints to compare with my num list?

1
  • 2
    list(ast.literal_eval(csvnumber[0])). Commented Jan 15, 2015 at 3:04

3 Answers 3

2

You can do it with list comprehensions and the int factory function:

[ int(i) for i in csvnumber[0].split(',') ]

Example

>>> csvnumber=['23,43,41,21,34']
>>> [ int(i) for i in csvnumber[0].split(',') ]
[23, 43, 41, 21, 34]
Sign up to request clarification or add additional context in comments.

Comments

1
x= ['23,43,41,21,34']

t=list(map(int, x[0].split(',')))

print (t)

Assume your list has only one element as your example.Output:

>>> 
[23, 43, 41, 21, 34]
>>> 

Then reach each element in the list t with a for loop and append them to your list num.

Comments

0

Just make a new list, split the current list at the commas, and append the items as integers like so:

new_csvnumber = []

for i in csvnumber[0].split(','):
    new_csvnumber.append(int(i))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.