Memo for creating a text formatting tool

A memo for making useful tools with just a bit of Python. Python is easy and nice. Assuming editing of NC program.

--Read all the contents of the file and throw it into the list

def readAll(fileName):
    with open(fileName, "r") as f
        ls = f.read().split("\n")
        return ls
#	f = open(fileName, "r")
#	ls = f.read().split("\n")
#	f.close()
	return ls

--Delete comments (characters in parentheses) with regular expressions

# 'O1000(SAMPLE)'
# → 'O1000'

import re

cmtptn = re.compile(r"\(.*?\)")
def removeComment(str):
	return cmtptn.sub("", str)

――Perspective with regular expressions

# 'G01X50.Y-50.Z-10.F500'
# → ['G01', 'X50.', 'Y-50.', 'Z-10.', 'F500']
ptr = re.compile(r"([A-Z#][^A-Z]+)")
def parse(str):
	p = ptr.findall(str)
	# print p
	return p

--Read the contents of the file received as an argument

import sys

if __name__ == "__main__":
	prm = sys.argv
	f1 = readAll(prm[1])

	for line in f1:
		print line
		# line = removeComment(line)
		# p = parse(line)

--Write to file

#It seems easier to redirect the printed one
# open/Use with to write (see readAll)
f = open(fname, "w")
f.write("foo")
f.close

--Dictionary

# coding: UTF-8

gcode = {
	"G00": u"Positioning"
	, "G01": u"Linear interpolation"
}

#When printing
print str(gcode).decode("unicode-escape")

--Only those that are included in the dictionary are parsed.

filtered = filter( lambda cd:gcode.has_key(cd) , parsed)

--Get display name from dictionary

dsp = map( lambda cd:gcode[cd], filtered)
print str(dsp).decode("unicode-escape")

--Meeting judgment

#It is delicate whether the judgment conditions are met
wptn = re.compile(r"M\d{3,}P\d{2,}")
def isWaitCode(str):
	return wptn.match(str)

--Standard output without line breaks

def sysout(v):
	sys.stdout.write(str(v))

#new line
def newline():
	sysout("\n")

Recommended Posts

Memo for creating a text formatting tool
[Python] Creating a scraping tool Memo
A tool for creating symbolic links on Windows
A memo for creating a python environment by a beginner
Step by Step for creating a Dockerfile
Commands for creating a new django project
A tool for easily entering Python code
Memo about Sphinx Part 1 (Creating a project)
Creating a cholera map for John Snow
Creating a development environment for machine learning
I made a useful tool for Digital Ocean
Created a header-only library management tool for C / C ++
Procedure for creating a LineBot made with Python
A memo when creating a python environment with miniconda
Commands for creating a python3 environment with virtualenv
Procedure for creating a Python quarantine environment (venv environment)
I made a user management tool for Let's Chat
I made a cleaning tool for Google Container Registry
A memo of installing Chainer 1.5 for GPU on Windows
Problems when creating a csv-json conversion tool with python
Make a CSV formatting tool with Python Pandas PyInstaller
Tool for creating training data for object detection in OpenCV
Procedure for creating a Line Bot on AWS Lambda
I made a repeating text data generation tool "rpttxt"
Memo for building a machine learning environment using Python
[Day 9] Creating a model
Creating a Home screen
4. Creating a structured program
[For memo] Linux Part 2
Text mining (for memos)
heroku memo (for myself)
Creating a dataset loader
About "Lamvery", a deployment and management tool for AWS Lambda
I tried using Tensorboard, a visualization tool for machine learning
Dockerfile for creating a data science environment based on pip3
[Python] What a programming inexperienced person did before creating a tool
[Addition] A memo for dividing a character string containing multiple spaces
(For beginners) Try creating a simple web API with Django
The story of releasing a Python text check tool on GitHub x CircleCI for the first time