如何使用python将CSV文件的第N行导入脚本

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

我正在Fusion360中使用一个名为importsplinecsv的脚本我想知道是否可以修改脚本,以便每10行导入一行?因为要导入的行数量非常大且膨胀。

如果我能得到一些很棒的帮助。

这里是文字

Author-Autodesk Inc。

从csv文件导入描述样条线

导入adsk.core,adsk.fusion,追溯导入io

def运行(上下文):ui =无尝试:app = adsk.core.Application.get()ui = app.userInterface#获取活动设计中的所有组件。产品= app.activeProduct设计= adsk.fusion.Design.cast(产品)title ='导入样条csv'如果没有设计:ui.messageBox(“无活动融合设计”,标题)返回

    dlg = ui.createFileDialog()
    dlg.title = 'Open CSV File'
    dlg.filter = 'Comma Separated Values (*.csv);;All Files (*.*)'
    if dlg.showOpen() != adsk.core.DialogResults.DialogOK :
        return

    filename = dlg.filename
    with io.open(filename, 'r', encoding='utf-8-sig') as f:
        points = adsk.core.ObjectCollection.create()
        line = f.readline()
        data = []
        while line:
            pntStrArr = line.split(',')
            for pntStr in pntStrArr:
                try:
                    data.append(float(pntStr))
                except:
                    break

            if len(data) >= 3 :
                point = adsk.core.Point3D.create(data[0], data[1], data[2])
                points.add(point)
            line = f.readline()
            data.clear()            
    if points.count:
        root = design.rootComponent
        sketch = root.sketches.add(root.xYConstructionPlane)
        sketch.sketchCurves.sketchFittedSplines.add(points)
    else:
        ui.messageBox('No valid points', title)            

except:
    if ui:
        ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
python csv spline pts fusion360
1个回答
0
投票

我以前没有使用过这个库,但是尝试:

for i, line in enumerate(f):
    if i%10==0:
        then your import command here

f是您的文件指针i将是行号,line将是您的行

  dlg = ui.createFileDialog()
    dlg.title = 'Open CSV File'
    dlg.filter = 'Comma Separated Values (*.csv);;All Files (*.*)'
    if dlg.showOpen() != adsk.core.DialogResults.DialogOK :
        return

    filename = dlg.filename
    with io.open(filename, 'r', encoding='utf-8-sig') as f:
        points = adsk.core.ObjectCollection.create()
        line = f.readline() <<<---- try here
        data = []
        while line:
            pntStrArr = line.split(',')
            for pntStr in pntStrArr:
                try:
                    data.append(float(pntStr))
                except:
                    break

            if len(data) >= 3 :
                point = adsk.core.Point3D.create(data[0], data[1], data[2])
                points.add(point)
            line = f.readline()
            data.clear()            
    if points.count:
        root = design.rootComponent
        sketch = root.sketches.add(root.xYConstructionPlane)
        sketch.sketchCurves.sketchFittedSplines.add(points)
    else:
        ui.messageBox('No valid points', title)            

except:
    if ui:
        ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
© www.soinside.com 2019 - 2024. All rights reserved.