2017-10-31 2 views
-1

목적 다음에 인용되지 않은 문자열로 튜플/목록을 수정 : 파이썬을 사용하여읽기 X는 Y, 파일의 Z이 목록

, .txt 파일에서 FreeCAD.Vector(223.90,67.99,45.00)를 얻을. 플로트 같은

1.0,8.2,9.888 
6.345,4.32,2.43 
... 

즉 X, Y, Z :

이 쉼표의 텍스트 파일 형태로 분리. 특정 응용 프로그램 "FreeCAD"는 기본 스크립팅 언어 (매크로)로 Python을 사용합니다. 기본 FreeCAD 객체에 대한 고유 메소드는 Vector입니다. FreeCAD.Vector 벡터는 (FreeCAD.Vector(105453.164062,90917.8671875,1274.77026367))입니다. 더 자세한 예를 들어

:

105507.460938 91080.125 1331.37109375 
105509.648438 91077.9375 1326.85534668 
105501.375 91072.890625 1318.00634766 
105487.0 91070.3984375 1318.89746094 

참고 : 여기에

>>> 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 파일의 예입니다 구분 기호가 쉼표 또는 공백이 될 수 있습니다 - 그것은 중요하지 않고이 될 수 있습니다

문제 :

이 값을 갖는 변수를 만들기

는 :

[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() 

:

은 일반적으로 하이브리드 문자열 목록을 만드는 방법의 문제는 나에게 어려운 것입니다.

나는이 단계에있어 또는 문제를 통해 작업 :

## Open the file with read only permit 
# f = open('C:/Users/Greg/Desktop/txt/pipe_example_s.txt', "r") 

## use readlines to read all lines in the file 
## The variable "lines" is a list containing all lines 
#lines = f.read().splitlines() 


with open('C:/Users/Greg/Desktop/txt/pipe_example_c.txt') as f: 
    mylist = [tuple(map(float, i.split(','))) for i in f] 

## close the file after reading the lines. 
#f.close() 
print mylist 
print "One" 
print mylist[0] 

p1 = 34.8999 
print "FreeCAD.Vector (%s)" % '34.8999, 2.8997, 3.09665' 

p1 = "FreeCAD.Vector (%s)" % '34.8999, 2.8997, 3.09665' 
print p1 

p2 = 123.456 
p1 = "FreeCAD.Vector (%s)" % (p2) + '34.8999, 2.8997, 3.09665' 
print p1 


p3 = tuple(mylist) 
print p3 

s = '(0.0034596999, 0.0034775001, 0.0010091923)' 
s = s.replace(',', 'FreeCAD.Vector ') 
print(s) # -> [0.0034596999 0.0034775001 0.0010091923] 

print mylist[1] 

출력 :

[(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)] 
One 
(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) 
+3

Google 도움말 센터를 확인하십시오. 특히, [Minimal, Complete and Verifiable Example] (https://stackoverflow.com/help/mcve)을 만드는 방법. 귀하는 너무 많은 정보를 주셨으므로 귀하의 질문은 상당히 불분명합니다. – bendl

+0

's = s.replace (',', FreeCAD.Vector ')'뒤에'(0.0034596999FreeCAD.Vector 0.0034775001FreeCAD.Vector 0.0010091923)'이 출력되어야합니다. 귀하의 질문의 끝. 귀하의 질문을 편집하고 발생하는 문제를 보여주는 ** 최소 ** 예제를 작성하십시오 (정확하게 그렇게 함). – martineau

+0

FreeCAD가 CSV에서 직접 가져올 수있는 것처럼 보입니다. 왜 코드를 작성해야합니까?아니면 FreeCAD Vectors를 만들지 않습니까? https://www.freecadweb.org/wiki/Spreadsheet_CSV – Wodin

답변

0

그것은 당신이 도움이 필요한 완전히 명확하지 않다, 그러나 아마이 당신에게 어떤을 줄 것이다 문제를 해결하는 데 도움이되는 아이디어 :

>>> 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