本文整理汇总了Python中clldutils.path.Path.open方法的典型用法代码示例。如果您正苦于以下问题:Python Path.open方法的具体用法?Python Path.open怎么用?Python Path.open使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类clldutils.path.Path
的用法示例。
在下文中一共展示了Path.open方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: write_data_file
# 需要导入模块: from clldutils.path import Path [as 别名]
# 或者: from clldutils.path.Path import open [as 别名]
def write_data_file(comment_text, overwrite):
lines = comment_text.split("\n")
filename = Path(lines[0].split(":",1)[1].strip())
if filename.exists() and not overwrite:
return "Embedded data file %s already exists! Run beastling with the --overwrite option if you wish to overwrite it.\n" % filename
if not filename.parent.exists():
filename.parent.mkdir()
with filename.open("w", encoding='utf8') as fp:
fp.write("\n".join(lines[1:]))
return "Wrote embedded data file %s.\n" % filename
示例2: load_normalized
# 需要导入模块: from clldutils.path import Path [as 别名]
# 或者: from clldutils.path.Path import open [as 别名]
def load_normalized(_path):
"""Normalization for quasi-identical strings which are often confused."""
path = Path(_path)
if not path.is_file():
path = local_path(_path)
norms = {}
with path.open(encoding='utf-8') as handle:
for line in handle:
if not line.startswith('#') and line.strip():
source, target = line.strip().split('\t')
norms[eval('"' + source + '"')] = eval('r"' + target + '"')
return norms
示例3: from_file
# 需要导入模块: from clldutils.path import Path [as 别名]
# 或者: from clldutils.path.Path import open [as 别名]
def from_file(cls, bibFile, encoding='utf8', lowercase=False):
"""Create bibtex database from a bib-file.
@param bibFile: path of the bibtex-database-file to be read.
"""
if not isinstance(bibFile, Path):
bibFile = Path(bibFile)
if bibFile.exists():
with bibFile.open(encoding=encoding) as fp:
content = fp.read()
else:
content = ''
return cls((Record.from_string('@' + m, lowercase=lowercase)
for m in re.split('^\s*@', content, 0, re.MULTILINE)))
示例4: load_alias
# 需要导入模块: from clldutils.path import Path [as 别名]
# 或者: from clldutils.path.Path import open [as 别名]
def load_alias(_path):
"""
Alias are one-character sequences which we can convert on a step-by step
basis by applying them successively to all subsegments of a segment.
"""
path = Path(_path)
if not path.is_file():
path = local_path(_path)
alias = {}
with path.open(encoding='utf-8') as handle:
for line in handle:
if not line.startswith('#') and line.strip():
source, target = line.strip().split('\t')
alias[eval('"' + source + '"')] = eval('r"' + target + '"')
return alias
示例5: create
# 需要导入模块: from clldutils.path import Path [as 别名]
# 或者: from clldutils.path.Path import open [as 别名]
def create(self, req, filename=None, verbose=True):
p = self.abspath(req)
if not p.parent.exists(): # pragma: no cover
p.parent.mkdir()
tmp = Path('%s.tmp' % p.as_posix())
if self.rdf:
# we do not create archives with a readme for rdf downloads, because each
# RDF entity points to the dataset and the void description of the dataset
# covers all relevant metadata.
#
# TODO: write test for the file name things!?
#
with closing(GzipFile(
filename=Path(tmp.stem).stem, fileobj=tmp.open('wb')
)) as fp:
self.before(req, fp)
for i, item in enumerate(page_query(self.query(req), verbose=verbose)):
self.dump(req, fp, item, i)
self.after(req, fp)
else:
with ZipFile(tmp.as_posix(), 'w', ZIP_DEFLATED) as zipfile:
if not filename:
fp = self.get_stream()
self.before(req, fp)
for i, item in enumerate(
page_query(self.query(req), verbose=verbose)):
self.dump(req, fp, item, i)
self.after(req, fp)
zipfile.writestr(self.name, self.read_stream(fp))
else: # pragma: no cover
zipfile.write(filename, self.name)
zipfile.writestr(
'README.txt',
README.format(
req.dataset.name,
'=' * (
len(req.dataset.name)
+ len(' data download')),
req.dataset.license,
TxtCitation(None).render(req.dataset, req)).encode('utf8'))
if p.exists(): # pragma: no cover
remove(p)
move(tmp, p)
示例6: write
# 需要导入模块: from clldutils.path import Path [as 别名]
# 或者: from clldutils.path.Path import open [as 别名]
def write(self, fname, **kw):
if not isinstance(fname, Path):
fname = Path(fname)
with fname.open('w', encoding='utf8') as fp:
fp.write(self.write_string(**kw))