Skip to main content

工具函数

复现python3的str.isascii()

def is_ascii(input_str):
try:
input_str.decode("ascii")
return True
except UnicodeDecodeError:
return False

def mxd_path_check(mxd_path):
if not is_ascii(mxd_path):
return mxd_path.decode("utf-8")

return mxd_path

使用json打印避免中文乱码

import json

# 字符串处理
res = "xxxx"
print(json.dumps(res, ensure_ascii=False))

# 文件处理
file = 'xx.json'
with open(file, 'r') as f:
print(json.dumps(json.load(f), ensure_ascii=False))

临时目录

import warnings as _warnings
from tempfile import mkdtemp

class TemporaryDirectory(object):
"""Create and return a temporary directory. This has the same
behavior as mkdtemp but can be used as a context manager. For
example:

with TemporaryDirectory() as tmpdir:
...

Upon exiting the context, the directory and everything contained
in it are removed.
"""

def __init__(self, suffix="", prefix="tmp", dir=None):
self._closed = False
self.name = None # Handle mkdtemp raising an exception
self.name = mkdtemp(suffix, prefix, dir)

def __repr__(self):
return "<{} {!r}>".format(self.__class__.__name__, self.name)

def __enter__(self):
return self.name

def cleanup(self, _warn=False):
if self.name and not self._closed:
try:
self._rmtree(self.name)
except (TypeError, AttributeError) as ex:
# Issue #10188: Emit a warning on stderr
# if the directory could not be cleaned
# up due to missing globals
if "None" not in str(ex):
raise
print(
"ERROR: {!r} while cleaning up {!r}".format(
ex,
self,
)
)
return
self._closed = True
if _warn:
self._warn("Implicitly cleaning up {!r}".format(self), ResourceWarning)

def __exit__(self, exc, value, tb):
self.cleanup()

def __del__(self):
# Issue a ResourceWarning if implicit cleanup needed
self.cleanup(_warn=True)

# XXX (ncoghlan): The following code attempts to make
# this class tolerant of the module nulling out process
# that happens during CPython interpreter shutdown
# Alas, it doesn't actually manage it. See issue #10188
_listdir = staticmethod(os.listdir)
_path_join = staticmethod(os.path.join)
_isdir = staticmethod(os.path.isdir)
_islink = staticmethod(os.path.islink)
_remove = staticmethod(os.remove)
_rmdir = staticmethod(os.rmdir)
_warn = _warnings.warn

def _rmtree(self, path):
# Essentially a stripped down version of shutil.rmtree. We can't
# use globals because they may be None'ed out at shutdown.
for name in self._listdir(path):
fullname = self._path_join(path, name)
try:
isdir = self._isdir(fullname) and not self._islink(fullname)
except OSError:
isdir = False
if isdir:
self._rmtree(fullname)
else:
try:
self._remove(fullname)
except OSError:
pass
try:
self._rmdir(path)
except OSError:
pass
# 使用
# 在临时目录中进行操作
with TemporaryDirectory() as tmp_dir:
tmp_dir....