It's a small story, but ...
Various operations are easy for python, and if you place the created module in the environment variable "PYTHONPATH" It is a convenient language when creating detailed scripts because it is easy to reuse.
In this article, about the functions that I create and use privately I will introduce it in addition to the memo (Since there are many ordinary things, I think there are many already mentioned ones)
import codecs
def pfreadlines(filename,encoding=None,**args):
with codecs.open(filename,Mode="r",encoding=encoding,**args) as f:
ret = f.readlines()
return ret
def pfwritelines(data,filename,encoding=None,**args):
with codecs.open(filename,Mode="w",encoding=encoding,**args) as f:
f.writelines(data)
You can write / read to a file by calling one of the above functions It seems that convenience will increase if you also create read / write according to the situation.
You can also use regular expressions, but when you don't have to go that far
def replace_all(src,new,*old):
for i in old:
src = src.replace(i,new)
return src
It seems to be confused because the argument is the opposite of the original replace ...
def dict_insert(src,key,value=True):
if(src.get(key) == None):
src[key] = value
This is useful when there are many operations like "when the target key cannot be found" (was)
If you have something like "This operation can be written like this" or "This library is so convenient that I recommend it" Please comment.
Recommended Posts