本文整理汇总了Python中json.html方法的典型用法代码示例。如果您正苦于以下问题:Python json.html方法的具体用法?Python json.html怎么用?Python json.html使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类json
的用法示例。
在下文中一共展示了json.html方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: json_to_dict
# 需要导入模块: import json [as 别名]
# 或者: from json import html [as 别名]
def json_to_dict(data):
"""Convert JSON to a dict().
See more about the json module at
https://docs.python.org/3.5/library/json.html
Parameters
----------
data : string
Data as a json-formatted string.
Returns
-------
dict
Data as Python Dictionary.
"""
try:
return json.loads(data)
except Exception as e:
raise e
示例2: dict_to_json
# 需要导入模块: import json [as 别名]
# 或者: from json import html [as 别名]
def dict_to_json(data):
"""Convert dict() to JSON-formatted string.
See more about the json module at
https://docs.python.org/3.5/library/json.html
Parameters
----------
data : dict
Data as Python Dictionary.
Returns
-------
string
Data as a json-formatted string.
"""
try:
return json.dumps(data, ensure_ascii=True, indent=2)
except Exception as e:
raise e
示例3: read_file
# 需要导入模块: import json [as 别名]
# 或者: from json import html [as 别名]
def read_file(filename, mode='r'):
"""Read in a file.
Parameters
----------
filename : string
Filename with full path.
mode : string
Read mode of file. Defaults to `r`. See more at
https://docs.python.org/3.5/library/functions.html#open
Returns
-------
string
Returns data as string.
"""
try:
with open(filename, mode) as f:
data = f.read()
return data
except IOError:
print('An error occured trying to read the file {}.'.format(filename))
except Exception as e:
raise e
示例4: write_file
# 需要导入模块: import json [as 别名]
# 或者: from json import html [as 别名]
def write_file(filename, data, mode='w'):
"""Write data in a file.
Parameters
----------
filename : string
Filename with full path.
data : string
Data to be stored.
mode : string
Read mode of file. Defaults to `w`. See more at
https://docs.python.org/3.5/library/functions.html#open
"""
try:
with open(filename, mode) as f:
f.write(data)
except IOError:
print('An error occured trying to write the file {}.'.format(filename))
except Exception as e:
raise e
示例5: read_file_json
# 需要导入模块: import json [as 别名]
# 或者: from json import html [as 别名]
def read_file_json(filename):
"""Read in a json file.
See more about the json module at
https://docs.python.org/3.5/library/json.html
Parameters
----------
filename : string
Filename with full path.
Returns
-------
dict
Data as a json-formatted string.
"""
try:
return json_to_dict(read_file(filename, 'r'))
except Exception as e:
raise e
示例6: read_file_csv
# 需要导入模块: import json [as 别名]
# 或者: from json import html [as 别名]
def read_file_csv(filename):
"""Read in CSV file.
See more at `csv.reader() <https://docs.python.org/3.5/library/csv.html>`_.
Parameters
----------
filename : string
Full filename with path of file.
Returns
-------
reader
Reader object, which can be iterated over.
"""
try:
with open(filename, newline='') as csvfile:
return csv.reader(csvfile, delimiter=',', quotechar='"')
except Exception as e:
raise e
finally:
csvfile.close()
示例7: dump
# 需要导入模块: import json [as 别名]
# 或者: from json import html [as 别名]
def dump(obj, fp, *args, **kwargs):
"""Serialize ``obj`` as a JSON formatted stream to ``fp``
:param obj: object to be serialized
:param fp: a ``.write()``-supporting file-like object
:param default: function that returns a serializable version of an object,
:func:`to_primitive` is used by default.
:param args: extra arguments, please see documentation \
of `json.dump <https://docs.python.org/2/library/json.html#basic-usage>`_
:param kwargs: extra named parameters, please see documentation \
of `json.dump <https://docs.python.org/2/library/json.html#basic-usage>`_
.. versionchanged:: 1.3
The *default* parameter now uses :func:`to_primitive` by default.
"""
default = kwargs.get('default', to_primitive)
return json.dump(obj, fp, default=default, *args, **kwargs)
示例8: default
# 需要导入模块: import json [as 别名]
# 或者: from json import html [as 别名]
def default(self, obj):
"""Handle special types for json.dumps.
If obj is a model from dokomoforms.models, return a dictionary
representation.
If obj is a datetime.date or datetime.time, return an
ISO 8601 representation string.
If obj is a psycpg2 Range, return its string representation.
Otherwise, throw a TypeError.
See
https://docs.python.org/3/library/json.html#json.JSONEncoder.default
"""
try:
return jsonify(obj, raise_exception=True)
except NotJSONifiableError:
return super().default(obj)
示例9: _json_default
# 需要导入模块: import json [as 别名]
# 或者: from json import html [as 别名]
def _json_default(obj):
"""
Coerce everything to strings.
All objects representing time get output as ISO8601.
"""
if isinstance(obj, (datetime.date, datetime.time, datetime.datetime)):
return obj.isoformat()
elif isinstance(obj, Exception):
return "Exception: %s" % str(obj)
return str(obj)
# skip natural LogRecord attributes
# http://docs.python.org/library/logging.html#logrecord-attributes
示例10: __init__
# 需要导入模块: import json [as 别名]
# 或者: from json import html [as 别名]
def __init__(self, *args, **kwargs):
"""
:param json_default: a function for encoding non-standard objects
as outlined in http://docs.python.org/2/library/json.html
:param json_encoder: optional custom encoder
:param json_serializer: a :meth:`json.dumps`-compatible callable
that will be used to serialize the log record.
:param prefix: an optional key prefix to nest logs
"""
self.json_default = kwargs.pop("json_default", _json_default)
self.json_encoder = kwargs.pop("json_encoder", None)
self.json_serializer = kwargs.pop("json_serializer", json.dumps)
self.default_values = kwargs.pop("default_extra", {})
self.prefix_key = kwargs.pop("prefix_key", "data")
logging.Formatter.__init__(self, *args, **kwargs)
self._fmt_parameters = self._parse_format_string()
self._skip_fields = set(self._fmt_parameters)
self._skip_fields.update(RESERVED_ATTRS)
示例11: default
# 需要导入模块: import json [as 别名]
# 或者: from json import html [as 别名]
def default(self, o: Any) -> Any: # pragma: no cover
"""Return serializable robot objects."""
# TODO: use @overload to split function
# BODY: Reduces cyclomatic complexity; but requires NumPy typing
if isinstance(o, np.ndarray):
return o.tolist()
elif isinstance(o, np.random.RandomState):
return None
elif isinstance(o, np.generic):
return str(o)
else:
try:
o = o.__dict__
except AttributeError:
pass
else:
return o
# let the base class default method raise the TypeError
# https://docs.python.org/3/library/json.html
return json.JSONEncoder.default(self, o)
示例12: clean_doi
# 需要导入模块: import json [as 别名]
# 或者: from json import html [as 别名]
def clean_doi(dirty_doi, return_none_if_error=False):
if not dirty_doi:
if return_none_if_error:
return None
else:
raise NoDoiException("There's no DOI at all.")
dirty_doi = dirty_doi.strip()
dirty_doi = dirty_doi.lower()
# test cases for this regex are at https://regex101.com/r/zS4hA0/1
p = re.compile(ur'(10\.\d+\/[^\s]+)')
matches = re.findall(p, dirty_doi)
if len(matches) == 0:
if return_none_if_error:
return None
else:
raise NoDoiException("There's no valid DOI.")
match = matches[0]
match = remove_nonprinting_characters(match)
try:
resp = unicode(match, "utf-8") # unicode is valid in dois
except (TypeError, UnicodeDecodeError):
resp = match
# remove any url fragments
if u"#" in resp:
resp = resp.split(u"#")[0]
# remove double quotes, they shouldn't be there as per http://www.doi.org/syntax.html
resp = resp.replace('"', '')
# remove trailing period, comma -- it is likely from a sentence or citation
if resp.endswith(u",") or resp.endswith(u"."):
resp = resp[:-1]
return resp
示例13: get_random_dois
# 需要导入模块: import json [as 别名]
# 或者: from json import html [as 别名]
def get_random_dois(n, from_date=None, only_journal_articles=True):
dois = []
while len(dois) < n:
# api takes a max of 100
number_this_round = min(n, 100)
url = u"https://api.crossref.org/works?sample={}".format(number_this_round)
if only_journal_articles:
url += u"&filter=type:journal-article"
if from_date:
url += u",from-pub-date:{}".format(from_date)
print url
print "calling crossref, asking for {} dois, so far have {} of {} dois".format(
number_this_round, len(dois), n)
r = requests.get(url)
items = r.json()["message"]["items"]
dois += [item["DOI"].lower() for item in items]
return dois
# from https://github.com/elastic/elasticsearch-py/issues/374
# to work around unicode problem
# class JSONSerializerPython2(elasticsearch.serializer.JSONSerializer):
# """Override elasticsearch library serializer to ensure it encodes utf characters during json dump.
# See original at: https://github.com/elastic/elasticsearch-py/blob/master/elasticsearch/serializer.py#L42
# A description of how ensure_ascii encodes unicode characters to ensure they can be sent across the wire
# as ascii can be found here: https://docs.python.org/2/library/json.html#basic-usage
# """
# def dumps(self, data):
# # don't serialize strings
# if isinstance(data, elasticsearch.compat.string_types):
# return data
# try:
# return json.dumps(data, default=self.default, ensure_ascii=True)
# except (ValueError, TypeError) as e:
# raise elasticsearch.exceptions.SerializationError(data, e)
示例14: get_tree
# 需要导入模块: import json [as 别名]
# 或者: from json import html [as 别名]
def get_tree(page):
page = page.replace(" ", " ") # otherwise starts-with for lxml doesn't work
try:
tree = html.fromstring(page)
except (etree.XMLSyntaxError, etree.ParserError) as e:
print u"not parsing, beause etree error in get_tree: {}".format(e)
tree = None
return tree
示例15: consume
# 需要导入模块: import json [as 别名]
# 或者: from json import html [as 别名]
def consume(self, subscription_name, callback, scheduler):
"""Begin listening to topic from the SubscriberClient.
:param subscription_name: str Subscription name
:param callback: Function which act on a topic message
:param scheduler: `Thread pool-based scheduler.<https://googleapis.dev/python/pubsub/latest/subscriber/api/scheduler.html?highlight=threadscheduler#google.cloud.pubsub_v1.subscriber.scheduler.ThreadScheduler>`_ # noqa
:return: `Future <https://googleapis.github.io/google-cloud-python/latest/pubsub/subscriber/api/futures.html>`_ # noqa
"""
subscription_path = self._client.subscription_path(
self._gc_project_id, subscription_name
)
return self._client.subscribe(
subscription_path, callback=callback, scheduler=scheduler
)