简体   繁体   English

Python:创建坐标列表(将字符串转换为int)

[英]Python: Create coordinate list (convert string to int)

I want to import several coordinates (could add up to 20.000) from an text file. 我想从一个文本文件中导入几个坐标(最多可以添加20.000)。 These coordinates need to be added into a list, looking like the follwing: 这些坐标需要添加到列表中,如下所示:

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

However when i want to import the coordinates i got the follwing error: 但是,当我要导入坐标时,出现以下错误:

invalid literal for int() with base 10

I can't figure out how to import the coordinates correctly. 我不知道如何正确导入坐标。 Does anyone has any suggestions why this does not work? 没有人有任何建议,为什么这不起作用? I think there's some problem with creating the integers. 我认为创建整数存在一些问题。 I use the following script: 我使用以下脚本:

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

The coordinates needed to be imported are looking like (this is "Bronbestand" in the script): 需要导入的坐标看起来像(脚本中的“ 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

Thus the script should result in: 因此,脚本应导致:

[[-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]]

I also tried importing the coordinates with the native python csv parser but this didn't work either. 我也尝试使用本机python csv解析器导入坐标,但这也不起作用。

Thank you all in advance for the help! 预先感谢大家的帮助!

Your numbers are not integers so the conversion to int fails. 您的数字不是整数,因此转换为int失败。

Try using float(i) instead of int(i) to convert into floating point numbers instead. 尝试使用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

Other answers have said why your script fails, however, there is another issue here - you are massively reinventing the wheel. 其他答案也说明了您的脚本为什么失败的原因,但是,这里还有另一个问题-您正在大量重新设计轮子。

This whole thing can be done in a couple of lines using the csv module and a list comprehension : 整个过程可以使用csv模块列表 csv在几行中完成:

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])

Gives us: 给我们:

[[-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]]

We open the file, make a csv.reader() to parse the csv file, skip the header row, then make a list of the numbers parsed as floats, ignoring the first column. 我们打开文件,创建一个csv.reader()来解析csv文件,跳过标题行,然后创建一个解析为浮点数的列表,忽略第一列。

As pointed out in the comments, as you are dealing with a lot of data, you may wish to iterate over the data lazily. 正如评论中指出的那样,当您处理大量数据时,您可能希望延迟迭代数据。 While making a list is good to test the output, in general, you probably want a generator rather than a list. 虽然创建列表可以很好地测试输出,但通常来说,您可能需要生成器而不是列表。 Eg: 例如:

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

Note that the file will need to remain open while you utilize this generator (remain inside the with block). 请注意,在使用此生成器时,文件将需要保持打开状态(保留在with块中)。

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

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