3

I have encountered the below error message:

invalid literal for int() with base 10: '"2"'

The 2 is enclosed by single quotes on outside, and double quotes on inside. This data is in the primes list from using print primes[0].

Sample data in primes list:

["2","3","5","7"]

The primes list is created from a CSV file via:

primes=csvfile.read().replace('\n',' ').split(',')

I am trying to trying to convert strings in primes list into integers.

Via Google I have come across similar questions to mine on SE, and I have tried the two common answers that are relevant to my problem IMO.

Using map():

primes=map(int,primes)

Using list comprehension:

primes=[int(i) for i in primes]

Unfortunately when I use either of them these both give the same error message as listed above. I get a similar error message for long() when used instead of int().

Please advise.

7
  • 3
    Your csv has a bunch of prime numbers in quotation marks? The issue is the quotation marks. Just remove the quotation marks. Commented Oct 11, 2017 at 18:46
  • 1
    use csv module, quotes will be stripped and you'll be able to convert the numbers Commented Oct 11, 2017 at 18:47
  • 2
    Can you give us some idea as to what the input looks like? For example, what is the output of primes=csvfile.read().replace('\n',' '); print(primes[:160])? Commented Oct 11, 2017 at 18:54
  • can you show a sample of your input? Commented Oct 11, 2017 at 19:32
  • Sorry but you should provide sample data here -1. How you get upvotes for this I don't know. Commented Oct 11, 2017 at 19:47

3 Answers 3

3

you want:

  • to read each csv lines
  • to create a single list of integers with the flattened version of all lines.

So you have to deal with the quotes (sometimes they may even not be here depending on how the file is created) and also when you're replacing linefeed by space, that doesn't split the last number from one line with the first number of the next line. You have a lot of issues.

Use csv module instead. Say f is the handle on the opened file then:

import csv

nums = [int(x) for row in csv.reader(f) for x in row]

that parses the cells, strips off the quotes if present and flatten + convert to integer, in one line.

To limit the number of numbers read, you could create a generator comprehension instead of a list comprehension and consume only the n first items:

n = 20000 # number of elements to extract
z = (int(x) for row in csv.reader(f) for x in row)
nums = [next(z) for _ in xrange(n)] # xrange => range for python 3

Even better, to avoid StopIteration exception you could use itertools.islice instead, so if csv data ends, you get the full list:

nums = list(itertools.islice(z,n))

(Note that you have to rewind the file to call this code more than once or you'll get no elements)

Performing this task without the csv module is of course possible ([int(x.strip('"')) for x in csvfile.read().replace('\n',',').split(',')]) but more complex and error-prone.

Sign up to request clarification or add additional context in comments.

9 Comments

I understand your code imports all primes in the file. How would I limit the number of elements imported? I have millions of primes in prime file - how could I limit number of elements imported to eg 200,000?
Nice. And use xrange instead of range for python2?
@unseen_rider yes, will save some memory in that case. since this is tagged python 2.7 I'll add that.
I get a StopIteration exception after running this code.
of course, if your file is too small. see my edit, I have added a default param to next , you'll get zeroes now, if your file is too small.
|
0

You can try this:

primes=csvfile.read().replace('\n',' ').split(',')
final_primes = [int(i[1:-1]) for i in primes]

6 Comments

I don't think that will work, because it splits according to commas so last number & first number of the next line are not splitted
Downvoting, because this answer is "Try this: {code}". I'll remove the downvote if you edit to something explained better.
@Darthfett not downvoting because I hate doing that while answering the same question, but I highty doubt that it works, for the reason stated above in my comment.
@Jean-FrançoisFabre Your solution is certainly the most robust; however, since the OP did not post a sample of his file I cannot really tell.
@Jean-FrançoisFabre Same, I gave you an upvote because from the current state of OP's question, it looks like this is what he needs. I'd remove the downvote on this answer if it actually explains what the intent was (but I will only upvote if it looks correct).
|
0

try this:

import csv

with open('csv.csv') as csvfile:
    data = csv.reader(csvfile, delimiter=',', skipinitialspace=True)
    primes = [int(j) for i in data for j in i]
    print primes

or to avoid duplicates

    print set(primes)

5 Comments

try this and get "TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'"
@Jean-FrançoisFabre can you showme your data input?
no need: i is a list because data is a csv.reader. Have you tested this?
but the question never says the format of the csv, is rows or colls or both
since OP is splitting according to commas, I guess that there are more than 1 column.

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.