【问题标题】:Python: Create coordinate list (convert string to int)Python:创建坐标列表(将字符串转换为 int)
【发布时间】:2013-01-13 13:22:25
【问题描述】:

我想从一个文本文件中导入多个坐标(加起来可以达到 20.000)。 这些坐标需要添加到列表中,如下所示:

coords = [[0,0],[1,0],[2,0],[0,1],[1,1],[2,1],[0,2],[1,2],[2,2]]

但是,当我想导入坐标时,我得到了以下错误:

invalid literal for int() with base 10

我不知道如何正确导入坐标。 有没有人有任何建议为什么这不起作用? 我认为创建整数存在一些问题。 我使用以下脚本:

Bronbestand = open("D:\\Documents\\SkyDrive\\afstuderen\\99 EEM - Abaqus 6.11.2\\scripting\\testuitlezen4.txt", "r")
headerLine = Bronbestand.readline()
valueList = headerLine.split(",")

xValueIndex = valueList.index("x")
#xValueIndex = int(xValueIndex)
yValueIndex = valueList.index("y")
#yValueIndex = int(yValueIndex)

coordList = []

for line in Bronbestand.readlines():
    segmentedLine = line.split(",")
    coordList.extend([segmentedLine[xValueIndex], segmentedLine[yValueIndex]])

coordList = [x.strip(' ') for x in coordList]
coordList = [x.strip('\n') for x in coordList]

coordList2 = []
#CoordList3 = [map(int, x) for x in coordList]

for i in coordList:
    coordList2 = [coordList[int(i)], coordList[int(i)]]

print "coordList = ", coordList
print "coordList2 = ", coordList2
#print "coordList3 = ", coordList3

需要导入的坐标看起来像(这是脚本中的“Bronbestand”):

id,x,y,
      1,  -1.24344945,   4.84291601
      2,  -2.40876842,   4.38153362
      3,  -3.42273545,    3.6448431
      4,  -4.22163963,   2.67913389
      5,   -4.7552824,   1.54508495
      6,  -4.99013376, -0.313952595
      7,   -4.7552824,  -1.54508495
      8,  -4.22163963,  -2.67913389
      9,  -3.42273545,   -3.6448431

因此脚本应该导致:

[[-1.24344945, 4.84291601],[-2.40876842, 4.38153362],[-3.42273545, 3.6448431],[-4.22163963, 2.67913389],[-4.7552824, 1.54508495],[-4.99013376,-0.313952595],[-4.7552824, -1.54508495],[-4.22163963, -2.67913389],[-3.42273545, -3.6448431]]

我也尝试使用本机 python csv 解析器导入坐标,但这也不起作用。

提前感谢大家的帮助!

【问题讨论】:

  • csv 模块存在时为什么要手动解析文件?
  • 请不要使用反斜杠。您可以始终在路径名中使用正斜杠。或者您可以使用没有转义反斜杠的r'....' 字符串(除非它们位于字符串的末尾)

标签: python list int


【解决方案1】:

您的数字不是整数,因此转换为 int 失败。

尝试使用 float(i) 而不是 int(i) 来转换为浮点数。

>>> int('1.5')

Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    int('1.5')
ValueError: invalid literal for int() with base 10: '1.5'
>>> float('1.5')
1.5

【讨论】:

  • +1,这很好地解释了为什么原始代码会失败。
【解决方案2】:

Other answers 已经说明了您的脚本失败的原因,但是,这里还有另一个问题 - 您正在大规模地重新发明轮子。

这整个事情可以使用the csv modulelist comprehension 用几行代码完成:

import csv

with open("test.csv") as file:
    data = csv.reader(file)
    next(data)
    print([[float(x) for x in line[1:]] for line in data])

给我们:

[[-1.24344945, 4.84291601], [-2.40876842, 4.38153362], [-3.42273545, 3.6448431], [-4.22163963, 2.67913389], [-4.7552824, 1.54508495], [-4.99013376, -0.313952595], [-4.7552824, -1.54508495], [-4.22163963, -2.67913389], [-3.42273545, -3.6448431]]

我们打开文件,创建 csv.reader() 来解析 csv 文件,跳过标题行,然后列出解析为浮点数的数字列表,忽略第一列。

正如 cmets 中所指出的,当您处理大量数据时,您可能希望懒惰地迭代数据。虽然制作一个列表可以很好地测试输出,但一般来说,您可能需要一个生成器而不是一个列表。例如:

([float(x) for x in line[1:]] for line in data)

请注意,当您使用此生成器时,文件需要保持打开状态(保留在 with 块内)。

【讨论】:

  • 这将是我的解决方案,只有我会将其作为生成器生成项目的函数,而不是打印它。我也不会使用float,因为它在处理精确点和数学时不准确,而是使用decimal
  • @InbarRose 当然,列表推导可以简单地更改为生成器表达式,具体取决于用例(对于打印,列表推导使输出清晰)。至于floatdecimal,这完全取决于用例。在大多数情况下,decimal 可能是矫枉过正。
  • Python 浮点数实际上是双精度数,所以小数点后 8 位不会超出它们。
  • 我不知道你为什么坚持decimal在这里是必要的。它更慢,除非证明需要这种精度,否则为什么要使用它?它只是添加更多代码而没有真正的好处。
  • @InbarRose:除非您要求控制应用程序的浮点计算精度,否则decimal 是多余的。 float() 更快、更简单,在大多数情况下,这正是您所需要的。
猜你喜欢
  • 2022-11-17
  • 2022-11-17
  • 1970-01-01
  • 2016-04-14
  • 1970-01-01
  • 1970-01-01
  • 2017-10-12
  • 2018-04-08
相关资源
最近更新 更多