ich möchte einen Wert, der mitunter als "20.53" daherkommt auf int wandeln
und ihn spaeter in einen zusammengestzten String ausgeben.
Da ich bei der Zusammensetzung eines Strins mittels "," immer ein Leerzeichen
von Python spendiert bekomme, habe ich mich fuer das "+" entschieden.
Denn da gibt es kein zusaetzliches Leerzeichen.
Klar, dass Python keine Zahl auf einen String addieren kann.
Also aus dem Int, was vorher ein Float war, einen String machen.
Gibt es eine Alternative zu dem hier?
Code: Alles auswählen
str(int(float(self.feedrate)))
Falls der komplette Code von Belang ist:
Code: Alles auswählen
teststring1 = "G1 Y20.322 F12.1 X300"
teststring2 = "G01 X20.322 Y12.1 I30 J35 F300"
class translate():
GCommandNotSupported = False
MCommandNotSupported = False
supportedGCommands = ["G01", "G1", "G04", "G4", "G28", "G90", "G91", "G92", "G999"]
supportedMCommands = ["M18", "M100", "M114"]
coord = [0.0, 0.0]
feedrate = 0
def getCleanedCode(self, line):
splitted = line.split(" ")
cmd = splitted[0]
if cmd.startswith("G"):
if cmd in self.supportedGCommands:
if splitted[1]:
firstval = splitted[1]
if firstval.startswith("X"): self.coord[0] = firstval[1:]
if firstval.startswith("Y"): self.coord[1] = firstval[1:]
if firstval.startswith("F"): self.feedrate = firstval[1:]
if splitted[2]:
secondval = splitted[2]
if secondval.startswith("X"): self.coord[0] = secondval[1:]
if secondval.startswith("Y"): self.coord[1] = secondval[1:]
if secondval.startswith("F"): self.feedrate = secondval[1:]
if splitted[3]:
thirdval = splitted[3]
if thirdval.startswith("X"): self.coord[0] = thirdval[1:]
if thirdval.startswith("Y"): self.coord[1] = thirdval[1:]
if thirdval.startswith("F"): self.feedrate = thirdval[1:]
rndcoord = self.getRounded(self.coord)
print 'DBG X' + str(rndcoord[0]) + ' Y' + str(rndcoord[1]) + ' F' + str(int(float(self.feedrate)))
else:
self.GCommandNotSupported = True
print "unsupported G"
elif cmd.startswith("M"):
print "MCommand"
if cmd in self.supportedMCommands:
print "supported M"
else:
self.MCommandNotSupported = True
print "unsupported M"
else:
# error-routine
pass
print "Unsupported Command. Only G- or M-Commands accepted!"
def getRounded(self, __coord):
xcoord = format(round(float(__coord[0]), 2), '.2f')
ycoord = format(round(float(__coord[1]), 2), '.2f')
rndcoord = [xcoord, ycoord]
return rndcoord
a = translate()
a.getCleanedCode(teststring1)
a.getCleanedCode(teststring2)