简体   繁体   English

Python将列表中的字符串转换为数字

[英]Python converting strings in a list to numbers

I have encountered the below error message: 我遇到了以下错误消息:

invalid literal for int() with base 10: '"2"' int()的基数为10的文字无效:'“2”'

The 2 is enclosed by single quotes on outside, and double quotes on inside. 2在外面用单引号括起来,在里面用双引号括起来。 This data is in the primes list from using print primes[0] . 此数据位于使用print primes[0]primes列表中。

Sample data in primes list: primes列表中的样本数据:

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

The primes list is created from a CSV file via: primes列表是通过以下CSV文件从CSV文件创建的:

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

I am trying to trying to convert strings in primes list into integers. 我试图尝试将primes列表中的字符串转换为整数。

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. 通过谷歌,我在SE上遇到了类似的问题,我尝试了两个与我的问题IMO相关的常见答案。

Using map(): 使用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(). 当使用而不是int()时,我得到long()的类似错误消息。

Please advise. 请指教。

you want: 你要:

  • to read each csv lines 阅读每个csv行
  • 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. 请改用csv模块。 Say f is the handle on the opened file then: 假设f是打开文件的句柄,然后:

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个第一项:

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: 更好的是,为了避免StopIteration异常,您可以使用itertools.islice ,因此如果csv数据结束,您将获得完整列表:

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. 没有 csv模块的情况下执行此任务当然是可能的( [int(x.strip('"')) for x in csvfile.read().replace('\\n',',').split(',')] )但更复杂且容易出错。

You can try this: 你可以试试这个:

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

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM