简体   繁体   English

ValueError: 无法将字符串转换为浮点数:'-'

[英]ValueError: could not convert string to float: '-'

#turns the text file into a list with each coordinates on a seperate line.
with open('AV.txt', 'r') as file:
    file_data = file.read()
file_data = file_data.replace('],', ']\n')
with open('AV.txt', 'w') as file:
    file.write(file_data)

#puts the vertices into an array
array = []
with open('AV.txt') as f:
    for line in f:
        array.append(line.strip("\n"))

#individual vertices according to their axis
x_array = [i[1] for i in array]
y_array = [i[7] for i in array]
z_array = [i[13] for i in array]

#Finds the maximum, minimum, and median for the x vertices
max_x = -1000000000000
min_x = 1000000000000
x_array_sorted = sorted(x_array)
length_x_array = len(x_array_sorted)
half_point_x = length_x_array//2
median_x = x_array_sorted[half_point_x]

n = 0
for q in x_array:
    if float(x_array[n]) > float(max_x):
        max_x = x_array[n]
    if float(x_array[n]) < float(min_x):
        min_x = x_array[n]
    n = n + 1

When I run this code I get this value error当我运行此代码时,我收到此值错误

ValueError: could not convert string to float: '-'.

when it gets to当它到达

if float(x_array[n]) > float(max_x):

I don't know how to get my code to accept that some of the coordinates are negative and therefore have a '-' in front of them.我不知道如何让我的代码接受某些坐标是负的,因此在它们前面有一个“-”。 Any ideas?有任何想法吗? Thank you.谢谢你。

An example line of text in the file that is being opened and read: [27.36, -31.24, 26.03]正在打开和读取的文件中的示例文本行:[27.36, -31.24, 26.03]

To solve the problem you have, you can just convert the list comprehensions into ints.要解决您遇到的问题,您只需将列表推导式转换为整数即可。

#individual vertices according to their axis
x_array = [int(i[1]) for i in array]
y_array = [int(i[7]) for i in array]
z_array = [int(i[13]) for i in array]

The above code is iterating through array 3 times to create the three list items.上面的代码遍历array 3 次以创建三个列表项。 I recommend you to convert it into a single loop like this.我建议您将其转换为这样的单个循环。

for i in array:
    x_array.append(int(i[1])
    y_array.append(int(i[7])
    z_array.append(int(i[13])

Also, can I recommend that you minimize the I/O by processing the values in memory.另外,我可以建议您通过处理内存中的值来最小化 I/O。

For example: you can change the below code to fewer lines:例如:您可以将以下代码更改为更少的行:

If file_data is the information you read from AV.txt , then the below 3 lines of code will get you the list you are looking for.如果 file_data 是您从AV.txt读取的信息,那么下面的 3 行代码将为您提供您要查找的列表。

file_data = '[1, 2, 3, 4, 5][20, 59, 60, 80][30, 15, 16]'
file_data = file_data.replace(']',']\n')
arr = file_data.strip('\n')
print (arr)

The output for this will be:输出将是:

[1, 2, 3, 4, 5]
[20, 59, 60, 80]
[30, 15, 16]

You can replace most of the below code with the above code.你可以用上面的代码替换下面的大部分代码。

#turns the text file into a list with each coordinates on a seperate line.
with open('AV.txt', 'r') as file:
    file_data = file.read()
file_data = file_data.replace('],', ']\n')
with open('AV.txt', 'w') as file:
    file.write(file_data)

#puts the vertices into an array
array = []
with open('AV.txt') as f:
    for line in f:
        array.append(line.strip("\n"))

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

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