Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从文件中读取X、Y、Z,将元组/列表修改为不带引号的字符串,后跟列表_Python_String_List_Tuples_Coordinates - Fatal编程技术网

Python 从文件中读取X、Y、Z,将元组/列表修改为不带引号的字符串,后跟列表

Python 从文件中读取X、Y、Z,将元组/列表修改为不带引号的字符串,后跟列表,python,string,list,tuples,coordinates,Python,String,List,Tuples,Coordinates,目标: # -*- coding: utf-8 -*- # 10/26/2017 6:54:16 AM # For Python, PEP 8 has emerged as the style guide # http://docs.python-guide.org/en/latest/intro/learning/ # https://www.freecadweb.org/wiki/Code_snippets # https://github.com/FreeCAD/FreeCAD impor

目标:

# -*- coding: utf-8 -*-
# 10/26/2017 6:54:16 AM
# For Python, PEP 8 has emerged as the style guide
# http://docs.python-guide.org/en/latest/intro/learning/
# https://www.freecadweb.org/wiki/Code_snippets
# https://github.com/FreeCAD/FreeCAD
import FreeCAD,Draft,Arch
# In Python, all the statements indented by the same number of character spaces
# after a programming construct are considered to be part of a single block of code.
# Python uses indentation as its method of grouping statements.

## @package Makewire
#  \ingroup ARCH
#  \brief Creates DWire object from file of 3D coordinates
#
#  This program opens a file, reads each space delimited line
#  and draws a dwire object
__title__ = "DWire Import"
__author__ = "Greg Robinson"
__url__ = "http://Lucrosol.com"
# example that works
# p1 = FreeCAD.Vector(0,0,1)
# p2 = FreeCAD.Vector(1,1,2)
# p3 = FreeCAD.Vector(2,4,3)
# Draft.makeWire([p1,p2,p3],closed=False)
# https://www.freecadweb.org/wiki/Draft_API
# hard coded path & file of coordinates
#    Example:
#105507.460938 91080.125 1331.37109375
#105509.648438 91077.9375 1326.85534668
#105501.375 91072.890625 1318.00634766
#105487.0 91070.3984375 1318.89746094
#105482.851562 91068.8203125 1318.02026367
#105480.5625 91063.5234375 1318.0456543
#105480.351562 90950.0859375 1318.84057617
#105475.992188 90940.046875 1319.13378906
#105473.546875 90933.515625 1318.09472656
#105473.820312 90897.3359375 1321.03942871
#105473.820312 90897.3359375 1321.03942871
#105475.671875 90889.4140625 1276.28381348
#105454.164062 90909.0078125 1274.7479248
#105453.164062 90917.8671875 1274.77026367
# Find & Replace Examples
# x = [s.replace('a', 'b') for s in x]
# words = [w.replace('[br]', '<br />') for w in words]
#
# function readfile
def fileread():
    myfile = open("C:/Users/Greg/Desktop/txt/pipe_example.txt")
    # Create empty set
    global lines
    lines = []
    for l in myfile.readlines():
        lines.append(l)
    myfile.close()
fileread()
# End readfile function

# contents of file are now in list
# Note
# Lists are enclosed in square brackets ([ and ]) and tuples in parentheses (( and )).
numbers = []
dwire_list = []
# Process list from file into float then get them into vectors
for line in lines:
    newline = [float(x) for x in line.split()]
    dwire_list.append(newline)
# append the processed items together

a_list = []
coords = []
count = len(dwire_list)
while (count > 0):
   coord0 = FreeCAD.Vector(dwire_list[count - 1])
   coords.append(coord0)
   count = count - 1
#
print "Loop Executed, Dwire should have appeared on file"
# Creat Dwire
# Draft.makeWire(coords,closed=False)
#     "makePipe([baseobj,diameter,length,placement,name]): creates an pipe object from the given base object"
# Create Dwire
Draft.makeWire(points,closed=False,face=True,support=None)
print coords
line = Draft.makeWire(coords,closed=False,face=True,support=None)
FreeCAD.ActiveDocument.recompute()
使用Python,从.txt文件获取
FreeCAD.Vector(223.90,67.99,45.00)

逗号分隔的文本文件格式:

1.0,8.2,9.888
6.345,4.32,2.43
...
i、 e.X,Y,Z为浮点数。 特定应用程序“FreeCAD”使用Python作为基本脚本语言(宏) 原生FreeCAD对象的原生方法是
Vector
,即
FreeCAD.Vector
向量是(X,Y,Z),即
FreeCAD.vector(105453.16406290917.86718751274.77026367)

作为进一步的详细示例:

>>> points=[FreeCAD.Vector(-2.50563430786,0.3603053689,0.0),FreeCAD.Vector(-1.67500686646,0.959897279739,0.0),FreeCAD.Vector(-0.53083139658,0.393310427666,0.0),FreeCAD.Vector(0.767367601395,0.932393193245,0.0)]
>>> line = Draft.makeWire(points,closed=False,face=True,support=None)
>>> Draft.autogroup(line)
下面是
data.txt
文件的示例:

105507.460938 91080.125 1331.37109375
105509.648438 91077.9375 1326.85534668
105501.375 91072.890625 1318.00634766
105487.0 91070.3984375 1318.89746094
注意:分隔符可以是逗号或空格-它不重要,可以是逗号或空格

问题:

# -*- coding: utf-8 -*-
# 10/26/2017 6:54:16 AM
# For Python, PEP 8 has emerged as the style guide
# http://docs.python-guide.org/en/latest/intro/learning/
# https://www.freecadweb.org/wiki/Code_snippets
# https://github.com/FreeCAD/FreeCAD
import FreeCAD,Draft,Arch
# In Python, all the statements indented by the same number of character spaces
# after a programming construct are considered to be part of a single block of code.
# Python uses indentation as its method of grouping statements.

## @package Makewire
#  \ingroup ARCH
#  \brief Creates DWire object from file of 3D coordinates
#
#  This program opens a file, reads each space delimited line
#  and draws a dwire object
__title__ = "DWire Import"
__author__ = "Greg Robinson"
__url__ = "http://Lucrosol.com"
# example that works
# p1 = FreeCAD.Vector(0,0,1)
# p2 = FreeCAD.Vector(1,1,2)
# p3 = FreeCAD.Vector(2,4,3)
# Draft.makeWire([p1,p2,p3],closed=False)
# https://www.freecadweb.org/wiki/Draft_API
# hard coded path & file of coordinates
#    Example:
#105507.460938 91080.125 1331.37109375
#105509.648438 91077.9375 1326.85534668
#105501.375 91072.890625 1318.00634766
#105487.0 91070.3984375 1318.89746094
#105482.851562 91068.8203125 1318.02026367
#105480.5625 91063.5234375 1318.0456543
#105480.351562 90950.0859375 1318.84057617
#105475.992188 90940.046875 1319.13378906
#105473.546875 90933.515625 1318.09472656
#105473.820312 90897.3359375 1321.03942871
#105473.820312 90897.3359375 1321.03942871
#105475.671875 90889.4140625 1276.28381348
#105454.164062 90909.0078125 1274.7479248
#105453.164062 90917.8671875 1274.77026367
# Find & Replace Examples
# x = [s.replace('a', 'b') for s in x]
# words = [w.replace('[br]', '<br />') for w in words]
#
# function readfile
def fileread():
    myfile = open("C:/Users/Greg/Desktop/txt/pipe_example.txt")
    # Create empty set
    global lines
    lines = []
    for l in myfile.readlines():
        lines.append(l)
    myfile.close()
fileread()
# End readfile function

# contents of file are now in list
# Note
# Lists are enclosed in square brackets ([ and ]) and tuples in parentheses (( and )).
numbers = []
dwire_list = []
# Process list from file into float then get them into vectors
for line in lines:
    newline = [float(x) for x in line.split()]
    dwire_list.append(newline)
# append the processed items together

a_list = []
coords = []
count = len(dwire_list)
while (count > 0):
   coord0 = FreeCAD.Vector(dwire_list[count - 1])
   coords.append(coord0)
   count = count - 1
#
print "Loop Executed, Dwire should have appeared on file"
# Creat Dwire
# Draft.makeWire(coords,closed=False)
#     "makePipe([baseobj,diameter,length,placement,name]): creates an pipe object from the given base object"
# Create Dwire
Draft.makeWire(points,closed=False,face=True,support=None)
print coords
line = Draft.makeWire(coords,closed=False,face=True,support=None)
FreeCAD.ActiveDocument.recompute()
创建具有此值的变量:

[FreeCAD.Vector(-2.50563430786,0.3603053689,0.0),FreeCAD.Vector(-1.67500686646,0.959897279739,0.0),…

设计一种组装输入的方法,以便FreeCAD可以接受XYZ输入

到目前为止我的代码:

# -*- coding: utf-8 -*-
# 10/26/2017 6:54:16 AM
# For Python, PEP 8 has emerged as the style guide
# http://docs.python-guide.org/en/latest/intro/learning/
# https://www.freecadweb.org/wiki/Code_snippets
# https://github.com/FreeCAD/FreeCAD
import FreeCAD,Draft,Arch
# In Python, all the statements indented by the same number of character spaces
# after a programming construct are considered to be part of a single block of code.
# Python uses indentation as its method of grouping statements.

## @package Makewire
#  \ingroup ARCH
#  \brief Creates DWire object from file of 3D coordinates
#
#  This program opens a file, reads each space delimited line
#  and draws a dwire object
__title__ = "DWire Import"
__author__ = "Greg Robinson"
__url__ = "http://Lucrosol.com"
# example that works
# p1 = FreeCAD.Vector(0,0,1)
# p2 = FreeCAD.Vector(1,1,2)
# p3 = FreeCAD.Vector(2,4,3)
# Draft.makeWire([p1,p2,p3],closed=False)
# https://www.freecadweb.org/wiki/Draft_API
# hard coded path & file of coordinates
#    Example:
#105507.460938 91080.125 1331.37109375
#105509.648438 91077.9375 1326.85534668
#105501.375 91072.890625 1318.00634766
#105487.0 91070.3984375 1318.89746094
#105482.851562 91068.8203125 1318.02026367
#105480.5625 91063.5234375 1318.0456543
#105480.351562 90950.0859375 1318.84057617
#105475.992188 90940.046875 1319.13378906
#105473.546875 90933.515625 1318.09472656
#105473.820312 90897.3359375 1321.03942871
#105473.820312 90897.3359375 1321.03942871
#105475.671875 90889.4140625 1276.28381348
#105454.164062 90909.0078125 1274.7479248
#105453.164062 90917.8671875 1274.77026367
# Find & Replace Examples
# x = [s.replace('a', 'b') for s in x]
# words = [w.replace('[br]', '<br />') for w in words]
#
# function readfile
def fileread():
    myfile = open("C:/Users/Greg/Desktop/txt/pipe_example.txt")
    # Create empty set
    global lines
    lines = []
    for l in myfile.readlines():
        lines.append(l)
    myfile.close()
fileread()
# End readfile function

# contents of file are now in list
# Note
# Lists are enclosed in square brackets ([ and ]) and tuples in parentheses (( and )).
numbers = []
dwire_list = []
# Process list from file into float then get them into vectors
for line in lines:
    newline = [float(x) for x in line.split()]
    dwire_list.append(newline)
# append the processed items together

a_list = []
coords = []
count = len(dwire_list)
while (count > 0):
   coord0 = FreeCAD.Vector(dwire_list[count - 1])
   coords.append(coord0)
   count = count - 1
#
print "Loop Executed, Dwire should have appeared on file"
# Creat Dwire
# Draft.makeWire(coords,closed=False)
#     "makePipe([baseobj,diameter,length,placement,name]): creates an pipe object from the given base object"
# Create Dwire
Draft.makeWire(points,closed=False,face=True,support=None)
print coords
line = Draft.makeWire(coords,closed=False,face=True,support=None)
FreeCAD.ActiveDocument.recompute()
哪些产出:

,(102339.457605, 92623.63571, 1113.615987), (102339.991842, 92624.037633, 1112.722525), (102305.610817, 92666.076043, 1112.399744), (102306.151843, 92666.628226, 1112.942683), (102306.687078, 92666.984192, 1112.139385), (102323.38305, 92643.109377, 1112.511513), (102323.824052, 92643.468957, 1113.20053), (102324.553584, 92643.911892, 1112.594155), (102359.99434, 92614.715556, 1113.832716), (102359.998296, 92613.819679, 1113.790412), (102361.103982, 92613.907801, 1113.974154), (102361.095313, 92614.148855, 1129.857483), (102359.940314, 92614.062477, 1129.823867), (102360.029689, 92614.917234, 1130.076275), (102360.090856, 92614.626343, 1145.496362), (102360.454485, 92613.963864, 1145.244853), (102361.020532, 92614.211705, 1145.234733)]
一个
(102360.003871, 92614.733022, 1114.159952)
FreeCAD.Vector(34.8999、2.8997、3.09665)
FreeCAD.Vector(34.8999、2.8997、3.09665)
FreeCAD.Vector(123.456)34.8999、2.8997、3.09665
((102360.003871, 92614.733022, 1114.159952), (102360.045926, 92613.778689, 1114.097542), (102361.109418, 92613.926808, 1114.123386), (102360.90909, 92614.061128, 1144.246289), (102360.008406, 92614.203715, 1144.217125), (102360.073353, 92615.032739, 1145.531946), (102338.988007, 92623.107091, 1113.028396), (102339.457605, 92623.63571, 1113.615987), (102339.991842, 92624.037633, 1112.722525), (102305.610817, 92666.076043, 1112.399744), (102306.151843, 92666.628226, 1112.942683), (102306.687078, 92666.984192, 1112.139385), (102323.38305, 92643.109377, 1112.511513), (102323.824052, 92643.468957, 1113.20053), (102324.553584, 92643.911892, 1112.594155), (102359.99434, 92614.715556, 1113.832716), (102359.998296, 92613.819679, 1113.790412), (102361.103982, 92613.907801, 1113.974154), (102361.095313, 92614.148855, 1129.857483), (102359.940314, 92614.062477, 1129.823867), (102360.029689, 92614.917234, 1130.076275), (102360.090856, 92614.626343, 1145.496362), (102360.454485, 92613.963864, 1145.244853), (102361.020532, 92614.211705, 1145.234733))
(0.0034596999FreeCAD.Vector 0.0034775001FreeCAD.Vector 0.0010091923)
(102360.045926, 92613.778689, 1114.097542)

您需要什么帮助还不完全清楚,但这可能会给您一些想法,帮助您解决问题:

>>> def vector(x, y, z):
...     print("vector: x=%f, y=%f, z=%f" % (x, y, z))
... 
>>> vector(1, 2, 3)
vector: x=1.000000, y=2.000000, z=3.000000
>>> v = [1, 2, 3]
>>> vector(*v)
vector: x=1.000000, y=2.000000, z=3.000000

请查看我们的帮助中心。特别是如何创建。您提供的信息太多,因此您的问题相当不清楚。在
s=s.replace(',','FreeCAD.Vector')
之后,打印的结果应该是
(0.0034596999FreeCAD.Vector 0.0034775001FreeCAD.Vector 0.0010091923)
,而不是问题结尾处当前显示的内容。请回答您的问题并创建一个最小的示例,说明您遇到的问题(并且正确执行)。看起来FreeCAD可以直接从CSV导入。为什么您需要编写代码来执行此操作?或者这不创建FreeCAD矢量?