variabeln aus txt dateien entnehmen

Python auf Einplatinencomputer wie Raspberry Pi, Banana Pi / Python für Micro-Controller
Antworten
Morila
User
Beiträge: 8
Registriert: Freitag 4. März 2016, 11:30

Hi. Ich bin's nochmal Euer Morila

Ich bin ganz furchtbar schlecht in Python aber
Also ich würde folgendes gerne umsetzen:

Ich habe eine txt. Datei.
Die sieht etwa so aus:

Variablen:
A156 B45 c23 d78
A55 b56 c26 d47
A97 usw.

Ich würde gerne in einer while schleife bei dem ersten durchlauf die erste Zeile in variablen nutzen, in dem 2. Durchlauf die 2. Zeile usw.

Das jetzt ist nur ein Beispiel, aber so ähnlich sollte es laufen:

While (Bedingung z.b. 1==1) :
(variablen Zeile 1 aus txt-datei-XY)
A=156
B=45
C=23
D=78
Ergebnis= A+B*C
Print (Ergebnis)

Dann das ganze wiederholt mit der nächsten Zeile aus dem txt dokument.
Die schleife soll enden, wenn das txt Dokument als nächstes eine leere Zeile hat.

Ich bitte euch entweder um eine Anleitung, eine Erklärung oder um einen fertigen Code.(erklährung dabei währe nett)
Ich hoffe dadurch mehr zu lernen, damit ich euch in zukunft nicht mehr sooft nerven muss.

Ich hoffe auf eine schnelle antwort von euch.
Bis dahin schon mal vielen dank fürs lesen. Eu
Euer Morila
BlackJack

@Morila: Du willst da keine Variablen sondern ein Wörterbuch (`dict`) und keine ``while``-Schleife sondern eine ``for``-Schleife über die Zeilen der Datei.

Wie kommt denn die Datei zustande? Hast Du da Einfluss drauf? Es wäre deutlich einfacher ein standardisiertes Format zu verwenden statt sich selbst etwas zu basteln was man parsen muss. JSON zum Beispiel.
Morila
User
Beiträge: 8
Registriert: Freitag 4. März 2016, 11:30

Naja. Es ist eine einfache g-code Datei.
In dem Format:
G01 X25 Y40
G00 Z13
Usw

In manchen Zeilen ist nur der z wert gegeben, im Rest x und y. Die länge der zahlen variiert.
Morila
User
Beiträge: 8
Registriert: Freitag 4. März 2016, 11:30

Der originalcode aus dem ich die werte auslesen möchte ist

( This file created by CNC_profile.rb )
( Cutter Diameter: 0.11811023622047245 )
( Stock Size: 4.0551181102362195 x 4.055118110236214 x 0.3937007874015748 )
G21 ( Unit of measure: millimeter )
G90 ( Absolute programming )
M03 ( Spindle on [clockwise] )

G00 Z13,000 F30 (Move safety height)
G00 X0.00 Y.00 F100 ( Move to origin )
G00 X-1,500 Y-1,500 F100
G01 Z0.0 F15
G01 X-1,500 Y-1,500 Z0,000 F50
G01 X-1,500 Y101,500 Z0,000 F50
G01 X101,500 Y101,500 Z0,000 F50
G01 X101,500 Y-1,500 Z0,000 F50
G01 X-1,500 Y-1,500 Z0,000 F50
G00 Z13,000 F30

G00 X0.00 Y.00 F100( Move to origin )
M05 ( Spindle stop )
M02 ( End of program )

end

Im grunde brauche ich auch nur den Mittelteil.
Danke für die schnelle holfe
__deets__
User
Beiträge: 14494
Registriert: Mittwoch 14. Oktober 2015, 14:29

Deine Problembeschreibungen sind wirklich unterirdisch. Bitte nicht irgendwelche vermeintlich äquivalenten Probleme praesentieren (A156 B45 c23 d78 ist ja nun kein Gcode, gell?), sondern dein wirkliches Problem benennen. Und auch, was du konkret erreichen moechtest. Was ist denn "nur den Mittelteil"? Die Anweisung in der Mitte? Die Anweisungen nach dem Spindle on, bis zum Spindle stop?

Computer sind *sehr* doof, und man muss ihnen ihre Aufgaben sehr, sehr praezise beschreiben. Und wenn du ein Programm schreiben moechtest, musst du so lernen zu denken, und deine Problem zu beschreiben. Sonst kann man da nichts machen, und endloses hin-und-her-Raten hier ist nervig fuer alle Beteiligten.
WernerSch
User
Beiträge: 13
Registriert: Sonntag 25. Februar 2018, 19:50

ich hatte da mal sowas geschrieben

Code: Alles auswählen

#Heidenhain nach Siemens Post
import sys , math , os ,re

dateii="37"
dateia="37"
dateinahmei=(dateii+".H")
dateinahmea=(dateia+".mpf")
e=open(dateinahmei,"r") #eingabedatei
f=open(dateinahmea,"w") #ausgabedatei
pi1=math.pi/180
zL=0
satz=1
zeil=0
retListe=[]
for linie in e.readlines():
        if zeil==2:
                text=linie
                posx=text.find("X")
                posy=text.find("Y")
                posz=text.find("Z")
                poseil=text.count("MAX")
                if poseil==1:
                        gcode="G0"
                        text=re.sub("MAX"," ",text)
                if poseil==0:
                        gcode="G1"
                posl=text.find("L")
                if posl>-1:
                        pose=text.find("/n")
                        posr0=text.count("R0")
                        if posr0>=1:
                                pose=text.find("R") 
                        if posx>-1:
                                tempX1=text[posx:posy]
                                tempX=float(tempX1.strip("X"))
                        if posy>-1:     
                                tempY1=text[posy:pose]
                                tempY=float(tempY1.strip("Y"))
                        if posz==-1:
                                tempZ=100
                        f.write("N"+str(satz)+" "+gcode+" X"+str(tempX)+" Y"+str(tempY)+" Z"+str(tempZ)+str("\n"))
                        satz+=1
        if zeil>2:
                text=linie
                posend=text.count("M30")
                if posend==1:
                        break
                pose=text.find("/n")
                posl=text.find("L")
                if posl==-1: #suchen nach cc
                        poscc=(text.count("C"))
                        if poscc>1: #cc lesen
                                ccxp=text.find("X")
                                ccyp=text.find("Y")
                                tempccx=text[ccxp:ccyp]
                                tempccx1=float(tempccx.strip("X"))
                                tempccy=text[ccyp:pose]
                                tempccy1=float(tempccy.strip("Y"))
                        posc=(text.count(str("C")))
                        if posc==1: #suchen nach c
                                cxp=text.find("X")
                                cyp=text.find("Y")
                                drp=text.count("DR+")
                                drm=text.count("DR-")
                                dpos=text.find("D")
                                if drp==1:
                                        gcode="G3"
                                if drm==1:
                                        gcode="G2"
                                tempcx1=text[cxp:cyp]
                                tempcx=(float(tempcx1.strip("X")))
                                tempcy1=text[cyp:dpos]
                                tempcy=(float(tempcy1.strip("Y")))
                                f.write("N"+str(satz)+" "+gcode+" X"+str(tempcx)+" Y"+str(tempcy)+" I=AC("+str(tempccx1)+")"+" J=AC("+str(tempccy1)+")"+str("\n"))
                                satz+=1
                                tempX=tempcx
                                tempY=tempcy
                if posl>-1:
                        poseil=text.count("MAX")
                        if poseil==1:
                                gcode="G0"
                                text=re.sub("MAX"," ",text)
                        if poseil==0:
                                gcode="G1"
                        posx=text.find("X")
                        posy1=text.find("Y")
                        posz=text.find("Z")
                        posz1=text.find("Z")
                        if posx==-1:
                                tempXn=tempX
                        if posy1==-1:
                                tempYn=tempY
                                posy=text.find("Y")
                                if posy==-1:
                                        posy=text.find("R")
                                if posy==-1:
                                        posy=text.find("F")
                                if posy==-1:
                                        posy=text.find("\n")
                        if posz1==-1:
                                tempZn=tempZ
                        if posx>-1:
                                posy=text.find("Y")
                                if posy==-1:
                                        posy=text.find("Z")
                                if posy==-1:
                                        posy=text.find("R")
                                if posy==-1:
                                        posy=text.find("F")
                                if posy==-1:
                                        posy=text.find("\n")
                                tempXn1=text[posx:posy]
                                tempXn=float(tempXn1.strip("X"))
                        if posy1>-1:
                                posy=text.find("Y")                                
                                if posz==-1:
                                        posz1=text.find("R")
                                if posz1==-1:
                                        posz1=text.find("F")
                                tempYn1=text[posy:posz1]
                                tempYn=float(tempYn1.strip("Y"))
                        if posz>-1:
                                pose=text.find("R")
                                if pose==-1:
                                        pose=text.find("F")
                                if pose==-1:
                                        pose=text.find("\n")
                                tempZn1=text[posz:pose]
                                tempZn=float(tempZn1.strip("Z"))
                        f.write("N"+str(satz)+" "+gcode+" "+"X"+str(tempXn)+" Y"+str(tempYn)+" Z"+str(tempZn)+str("\n"))
                        satz+=1
                        tempX=tempXn
                        tempY=tempYn
                        tempZ=tempZn
        zeil+=1
        zL+=1
e.close()
f.close()
print("fertig")
quit
vielleicht hilft das noch
Antworten