如何使用python停止读取代码,直到空白行没有panda,行数可变



目前,这段代码只适用于一行,我如何让它循环/重复,以便可以对csv/excel格式的一系列行到一个空白行进行循环/重复?

import csv
def read_csv(file_name='filepath.csv'):
with open(file_name) as csv_file:
file = csv.reader(csv_file)
return [i for i in file]
csvFileArray = read_csv()
row = csvFileArray[1]  # first row
x, y, z, l, m, n, p, q, r = row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8]
x = float(x)
y = float(y)
z = float(z)
l = float(l)
m = float(m)
n = float(n)
p = float(p)
q = float(q)
r = float(r)
y_axis = Vector((l, m, n))
z_axis = Vector((p, q, r))
x_axis = y_axis.cross(z_axis).normalized()
M = Matrix((x_axis, y_axis, z_axis)).transposed()
M = M.to_4x4()
M.translation = scale * Vector((x, y, z))
#test
print(M)
print(x_axis, y_axis, z_axis)
bpy.ops.object.empty_add()
mt = context.object
mt.empty_display_type = 'ARROWS'
mt.matrix_world = M

尝试while循环,其中:while x!=0和y!=0。。。等等,它似乎不起作用?

系统控制台消息图像

csv图像部分

首先需要使用for循环对数据进行迭代。

接下来检查行的长度是否为> 0

您不需要定义每个row[i],只需解压缩整行(python的一个有用功能(

如果希望在空白行结束循环,请使用break结束循环。如果代码未评估为> 0,则代码将转到那里

csvFileArray = read_csv()
for row in csvFileArray[1:]: # iterate over the  row, ignore the first row (index 0) - start from index 1 -> last index
if len(row) > 0:
x, y, z, l, m, n, p, q, r = row # unpack the row - This is the same as x, y, z, l, m, n, p, q, r = row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8]
# Do calculations ... indent any logic involving the row items here.
# from your code I would assume it is as follows:
x = float(x)
y = float(y)
z = float(z)
l = float(l)
m = float(m)
n = float(n)
p = float(p)
q = float(q)
r = float(r)
y_axis = Vector((l, m, n))
z_axis = Vector((p, q, r))
x_axis = y_axis.cross(z_axis).normalized()
M = Matrix((x_axis, y_axis, z_axis)).transposed()
M = M.to_4x4()
M.translation = scale * Vector((x, y, z))
#test
print(M)
print(x_axis, y_axis, z_axis)
bpy.ops.object.empty_add()
mt = context.object
mt.empty_display_type = 'ARROWS'
mt.matrix_world = M
break # no need for an else clause but it is the same as having one.  This will stop the loop and end the routine.

最新更新