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

问题描述 投票:0回答:1

当前,此代码对于一行来说效果很好,我如何让它循环/重复,以便可以将一系列行做到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 ... etc似乎不起作用?

python excel loops csv repeat
1个回答
0
投票

您首先需要使用for循环遍历数据。

下一次检查行的长度为> 0

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

如果要在空白行结束,请使用break结束循环。如果代码的计算结果不为> 0

,则代码将进入该位置
csvFileArray = read_csv()
for row in csvFileArray: # iterate over the  row
    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.
© www.soinside.com 2019 - 2024. All rights reserved.