本文整理汇总了Python中io.TextIOWrapper方法的典型用法代码示例。如果您正苦于以下问题:Python io.TextIOWrapper方法的具体用法?Python io.TextIOWrapper怎么用?Python io.TextIOWrapper使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io
的用法示例。
在下文中一共展示了io.TextIOWrapper方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: popen
# 需要导入模块: import io [as 别名]
# 或者: from io import TextIOWrapper [as 别名]
def popen(cmd, mode="r", buffering=-1):
if not isinstance(cmd, str):
raise TypeError("invalid cmd type (%s, expected string)" % type(cmd))
if mode not in ("r", "w"):
raise ValueError("invalid mode %r" % mode)
if buffering == 0 or buffering is None:
raise ValueError("popen() does not support unbuffered streams")
import subprocess, io
if mode == "r":
proc = subprocess.Popen(cmd,
shell=True,
stdout=subprocess.PIPE,
bufsize=buffering)
return _wrap_close(io.TextIOWrapper(proc.stdout), proc)
else:
proc = subprocess.Popen(cmd,
shell=True,
stdin=subprocess.PIPE,
bufsize=buffering)
return _wrap_close(io.TextIOWrapper(proc.stdin), proc)
# Helper for popen() -- a proxy for a file whose close waits for the process
示例2: _fopen
# 需要导入模块: import io [as 别名]
# 或者: from io import TextIOWrapper [as 别名]
def _fopen(fname, mode):
"""
Extend file open function, to support
1) "-", which means stdin/stdout
2) "$cmd |" which means pipe.stdout
"""
if mode not in ["w", "r", "wb", "rb"]:
raise ValueError("Unknown open mode: {mode}".format(mode=mode))
if not fname:
return None
fname = fname.rstrip()
if fname == "-":
if mode in ["w", "wb"]:
return sys.stdout.buffer if mode == "wb" else sys.stdout
else:
return sys.stdin.buffer if mode == "rb" else sys.stdin
elif fname[-1] == "|":
pin = pipe_fopen(fname[:-1], mode, background=(mode == "rb"))
return pin if mode == "rb" else TextIOWrapper(pin)
else:
if mode in ["r", "rb"] and not os.path.exists(fname):
raise FileNotFoundError(
"Could not find common file: {}".format(fname))
return open(fname, mode)
示例3: test_write_exclusive_text_file
# 需要导入模块: import io [as 别名]
# 或者: from io import TextIOWrapper [as 别名]
def test_write_exclusive_text_file(smb_share):
file_path = "%s\\%s" % (smb_share, "file.txt")
file_contents = u"File Contents\nNewline"
with smbclient.open_file(file_path, mode='x') as fd:
assert isinstance(fd, io.TextIOWrapper)
assert fd.closed is False
with pytest.raises(IOError):
fd.read()
assert fd.tell() == 0
fd.write(file_contents)
assert int(fd.tell()) == (len(file_contents) - 1 + len(os.linesep))
assert fd.closed is True
with smbclient.open_file(file_path, mode='r') as fd:
assert fd.read() == file_contents
with pytest.raises(OSError, match=re.escape("[NtStatus 0xc0000035] File exists: ")):
smbclient.open_file(file_path, mode='x')
assert fd.closed is True
示例4: test_append_text_file
# 需要导入模块: import io [as 别名]
# 或者: from io import TextIOWrapper [as 别名]
def test_append_text_file(smb_share):
file_path = "%s\\%s" % (smb_share, "file.txt")
with smbclient.open_file(file_path, mode='a') as fd:
assert isinstance(fd, io.TextIOWrapper)
with pytest.raises(IOError):
fd.read()
fd.write(u"abc")
assert fd.tell() == 3
with smbclient.open_file(file_path, mode='a') as fd:
assert fd.tell() == 3
fd.write(u"def")
assert fd.tell() == 6
with smbclient.open_file(file_path, mode='r') as fd:
assert fd.read() == u"abcdef"
示例5: next_filehandle
# 需要导入模块: import io [as 别名]
# 或者: from io import TextIOWrapper [as 别名]
def next_filehandle(self):
"""Go to the next file and retrun its filehandle or None (meaning no more files)."""
filename = self.next_filename()
if filename is None:
fhandle = None
elif filename == '-':
fhandle = io.TextIOWrapper(sys.stdin.buffer, encoding=self.encoding)
elif filename == '<filehandle_input>':
fhandle = self.filehandle
else:
filename_extension = filename.split('.')[-1]
if filename_extension == 'gz':
myopen = gzip.open
elif filename_extension == 'xz':
myopen = lzma.open
elif filename_extension == 'bz2':
myopen = bz2.open
else:
myopen = open
fhandle = myopen(filename, 'rt', encoding=self.encoding)
self.filehandle = fhandle
return fhandle
示例6: toprettyxml
# 需要导入模块: import io [as 别名]
# 或者: from io import TextIOWrapper [as 别名]
def toprettyxml(self, indent="\t", newl="\n", encoding=None):
if encoding is None:
writer = io.StringIO()
else:
writer = io.TextIOWrapper(io.BytesIO(),
encoding=encoding,
errors="xmlcharrefreplace",
newline='\n')
if self.nodeType == Node.DOCUMENT_NODE:
# Can pass encoding only to document, to put it into XML header
self.writexml(writer, "", indent, newl, encoding)
else:
self.writexml(writer, "", indent, newl)
if encoding is None:
return writer.getvalue()
else:
return writer.detach().getvalue()
示例7: listImport
# 需要导入模块: import io [as 别名]
# 或者: from io import TextIOWrapper [as 别名]
def listImport(self, force=None, path=None):
_list = request.url_rule.split('/')[2]
file = request.files['file']
force = request.form.get('force')
count = wl.countWhitelist() if _list.lower == 'whitelist' else bl.countBlacklist()
if (count == 0) | (not count) | (force == "f"):
if _list.lower == 'whitelist':
wl.dropWhitelist()
wl.importWhitelist(TextIOWrapper(file.stream))
else:
bl.dropBlacklist()
bl.importBlacklist(TextIOWrapper(file.stream))
status = _list[0]+"l_imported"
else:
status = _list[0]+"l_already_filled"
return render_template('admin.html', status=status, **self.adminInfo())
# /admin/whitelist/export
# /admin/blacklist/export
示例8: save_uploaded_file
# 需要导入模块: import io [as 别名]
# 或者: from io import TextIOWrapper [as 别名]
def save_uploaded_file(request, process_records=None):
if len(request.FILES) != 1:
raise ValueError("Received %s files instead of 1" % len(request.FILES))
# parse file
stream = next(iter(request.FILES.values()))
filename = stream._name
if not filename.endswith('.xls') and not filename.endswith('.xlsx'):
stream = TextIOWrapper(stream.file, encoding = 'utf-8')
json_records = parse_file(filename, stream)
if process_records:
json_records = process_records(json_records, filename=filename)
# save json to temporary file
uploaded_file_id = hashlib.md5(str(json_records).encode('utf-8')).hexdigest()
serialized_file_path = _compute_serialized_file_path(uploaded_file_id)
with gzip.open(serialized_file_path, "wt") as f:
json.dump(json_records, f)
return uploaded_file_id, filename, json_records
示例9: request_list
# 需要导入模块: import io [as 别名]
# 或者: from io import TextIOWrapper [as 别名]
def request_list(
self, identity: Identity
) -> Iterator[Sequence[Mapping[str, object]]]:
team = self.team
if not (isinstance(team, identity.team_type) and
cast(str, identity.identifier).startswith(team.server_url)):
return
start = 0
while True:
response = self.request(
identity,
'GET',
self.LIST_URL.format(self.team, start)
)
assert response.code == 200
payload = json.load(io.TextIOWrapper(response, encoding='utf-8'))
response.close()
yield from payload['values']
if payload['isLastPage']:
break
start = payload['nextPageStart']
示例10: _python2_bz2open
# 需要导入模块: import io [as 别名]
# 或者: from io import TextIOWrapper [as 别名]
def _python2_bz2open(fn, mode, encoding, newline):
"""Wrapper to open bz2 in text mode.
Parameters
----------
fn : str
File name
mode : {'r', 'w'}
File mode. Note that bz2 Text files are not supported.
encoding : str
Ignored, text bz2 files not supported in Python2.
newline : str
Ignored, text bz2 files not supported in Python2.
"""
import bz2
_check_mode(mode, encoding, newline)
if "t" in mode:
# BZ2File is missing necessary functions for TextIOWrapper
warnings.warn("Assuming latin1 encoding for bz2 text file in Python2",
RuntimeWarning, stacklevel=5)
mode = mode.replace("t", "")
return bz2.BZ2File(fn, mode)
示例11: process_species
# 需要导入模块: import io [as 别名]
# 或者: from io import TextIOWrapper [as 别名]
def process_species(self, limit):
"""
Loop through the xml file and process the species.
We add elements to the graph, and store the
id-to-label in the label_hash dict.
:param limit:
:return:
"""
myfile = '/'.join((self.rawdir, self.files['data']['file']))
with gzip.open(myfile, 'rb') as readbin:
filereader = io.TextIOWrapper(readbin, newline="")
filereader.readline() # remove the xml declaration line
for event, elem in ET.iterparse(filereader):
# Species ids are == NCBITaxon ids
self.process_xml_table(
elem, 'Species_gb', self._process_species_table_row, limit)
示例12: parse
# 需要导入模块: import io [as 别名]
# 或者: from io import TextIOWrapper [as 别名]
def parse(input: TextIOWrapper, regexp: str, separator: str = None,
labels: Dict[str, str] = {}, metric_name_prefix: str = '') -> List[Metric]:
"""Custom parse function for gauge tpm from mysql.
TPM: 87060.0
TPM: 95220.0
TPM: 93600.0
TPM: 90000.0
"""
new_metrics = []
new_line = readline_with_check(input, EOF_line='end')
if "TPM:" in new_line:
regex = re.findall(r'TPM: (?P<tpm>\d*.\d*)', new_line)
tpm = float(regex[0])
new_metrics.append(Metric(metric_name_prefix + 'tpm', tpm,
type=MetricType.GAUGE, labels=labels,
help="TPM (transaction per minute) from mysql"))
return new_metrics
示例13: main
# 需要导入模块: import io [as 别名]
# 或者: from io import TextIOWrapper [as 别名]
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--config', help='Config file')
args = parser.parse_args()
if args.config:
with open(args.config) as input:
config = json.load(input)
else:
config = {}
input = io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8')
state = persist_lines(config, input)
emit_state(state)
logger.debug("Exiting normally")
示例14: __enter__
# 需要导入模块: import io [as 别名]
# 或者: from io import TextIOWrapper [as 别名]
def __enter__(self):
mode = self.mode.replace("t", "").replace("b", "") + "b"
f = self.fs.open(self.path, mode=mode)
self.fobjects = [f]
if self.compression is not None:
compress = compr[self.compression]
f = compress(f, mode=mode[0])
self.fobjects.append(f)
if "b" not in self.mode:
# assume, for example, that 'r' is equivalent to 'rt' as in builtin
f = io.TextIOWrapper(
f, encoding=self.encoding, errors=self.errors, newline=self.newline
)
self.fobjects.append(f)
return self.fobjects[-1]
示例15: __init__
# 需要导入模块: import io [as 别名]
# 或者: from io import TextIOWrapper [as 别名]
def __init__(self, stream): # type: (io.TextIOWrapper) -> None
self._stream = stream