本文整理匯總了Python中csv.html方法的典型用法代碼示例。如果您正苦於以下問題:Python csv.html方法的具體用法?Python csv.html怎麽用?Python csv.html使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類csv
的用法示例。
在下文中一共展示了csv.html方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: json_to_dict
# 需要導入模塊: import csv [as 別名]
# 或者: from csv 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 csv [as 別名]
# 或者: from csv 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 csv [as 別名]
# 或者: from csv 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 csv [as 別名]
# 或者: from csv 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 csv [as 別名]
# 或者: from csv 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 csv [as 別名]
# 或者: from csv 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: write_unicode_csv
# 需要導入模塊: import csv [as 別名]
# 或者: from csv import html [as 別名]
def write_unicode_csv(filename, rows, delimiter=',', quotechar='"',
quoting=csv.QUOTE_MINIMAL, lineterminator='\n',
encoding='utf-8'):
# Python 3 version
if sys.version_info[0] >= 3:
# Open the file in text mode with given encoding
# Set newline arg to '' (see https://docs.python.org/3/library/csv.html)
with open(filename, 'w', newline='', encoding=encoding) as f:
# Next, get the csv writer, with unicode delimiter and quotechar
csv_writer = csv.writer(f, delimiter=delimiter, quotechar=quotechar,
quoting=quoting, lineterminator=lineterminator)
# Write the rows to the file
csv_writer.writerows(rows)
# Python 2 version
else:
# Open the file, no encoding specified
with open(filename, 'w') as f:
# Next, get the csv writer, passing delimiter and quotechar as
# bytestrings rather than unicode
csv_writer = csv.writer(f, delimiter=delimiter.encode(encoding),
quotechar=quotechar.encode(encoding),
quoting=quoting, lineterminator=lineterminator)
for row in rows:
csv_writer.writerow([unicode(cell).encode(encoding)
for cell in row])
示例8: read_csv
# 需要導入模塊: import csv [as 別名]
# 或者: from csv import html [as 別名]
def read_csv(handle):
""" Read CSV file
:param handle: File-like object of the CSV file
:return: csv.reader object
"""
# These functions are to handle unicode in Python 2 as described in:
# https://docs.python.org/2/library/csv.html#examples
def unicode_csv_reader(unicode_csv_data, dialect=csv.excel, **kwargs):
""" csv.py doesn't do Unicode; encode temporarily as UTF-8."""
csv_reader = csv.reader(utf_8_encoder(unicode_csv_data),
dialect=dialect, **kwargs)
for row in csv_reader:
# decode UTF-8 back to Unicode, cell by cell:
yield [unicode(cell, 'utf-8') for cell in row]
def utf_8_encoder(unicode_csv_data):
""" Encode with UTF-8."""
for line in unicode_csv_data:
yield line.encode('utf-8')
return unicode_csv_reader(handle) if PY2 else csv.reader(handle)
示例9: process
# 需要導入模塊: import csv [as 別名]
# 或者: from csv import html [as 別名]
def process(proc_data):
"""
Final processing to conform to the schema.
Parameters:
proc_data: (dictionary) raw structured data to process
Returns:
List of dictionaries. Each dictionary represents a row in the csv file:
[
{
csv file converted to a Dictionary
https://docs.python.org/3/library/csv.html
}
]
"""
# No further processing
return proc_data
示例10: unicode_csv_reader
# 需要導入模塊: import csv [as 別名]
# 或者: from csv import html [as 別名]
def unicode_csv_reader(unicode_csv_data: TextIOWrapper, **kwargs: Any) -> Any:
r"""Since the standard csv library does not handle unicode in Python 2, we need a wrapper.
Borrowed and slightly modified from the Python docs:
https://docs.python.org/2/library/csv.html#csv-examples
Args:
unicode_csv_data (TextIOWrapper): unicode csv data (see example below)
Examples:
>>> from torchaudio.datasets.utils import unicode_csv_reader
>>> import io
>>> with io.open(data_path, encoding="utf8") as f:
>>> reader = unicode_csv_reader(f)
"""
# Fix field larger than field limit error
maxInt = sys.maxsize
while True:
# decrease the maxInt value by factor 10
# as long as the OverflowError occurs.
try:
csv.field_size_limit(maxInt)
break
except OverflowError:
maxInt = int(maxInt / 10)
csv.field_size_limit(maxInt)
for line in csv.reader(unicode_csv_data, **kwargs):
yield line
示例11: _get_csv_
# 需要導入模塊: import csv [as 別名]
# 或者: from csv import html [as 別名]
def _get_csv_(self):
# todo: get the csv.reader to handle unicode as done here:
# http://docs.python.org/library/csv.html#examples
url = reverse('csv_export', kwargs={
'username': self.user.username, 'id_string': self.xform.id_string})
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
actual_csv = self._get_response_content(response)
actual_lines = actual_csv.split("\n")
return csv.reader(actual_lines)
示例12: read_csv_file_to_json_flexible
# 需要導入模塊: import csv [as 別名]
# 或者: from csv import html [as 別名]
def read_csv_file_to_json_flexible(file_name):
"""Reads a csv file to json
See https://docs.python.org/3/library/csv.html for options and formats, etc.
"""
import csv
with open(file_name, newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
yield row
示例13: read_tsv_file_to_json_flexible
# 需要導入模塊: import csv [as 別名]
# 或者: from csv import html [as 別名]
def read_tsv_file_to_json_flexible(file_name):
"""Reads a tsv file to json
See https://docs.python.org/3/library/csv.html for options and formats, etc.
"""
import csv
with open(file_name, newline='') as csvfile:
reader = csv.DictReader(csvfile, delimiter='\t')
for row in reader:
yield row
示例14: unicode_csv_reader
# 需要導入模塊: import csv [as 別名]
# 或者: from csv import html [as 別名]
def unicode_csv_reader(data):
"""
Handle Unicode CSV data
See: https://docs.python.org/2/library/csv.html
"""
def utf_8_encoder(unicode_csv_data):
for line in unicode_csv_data:
yield line.encode("utf-8")
csv_reader = csv.reader(utf_8_encoder(data))
for row in csv_reader:
yield [unicode(cell, "utf-8") for cell in row]
示例15: read_unicode_csv
# 需要導入模塊: import csv [as 別名]
# 或者: from csv import html [as 別名]
def read_unicode_csv(filename, delimiter=',', quotechar='"',
quoting=csv.QUOTE_MINIMAL, lineterminator='\n',
encoding='utf-8', skiprows=0):
# Python 3 version
if sys.version_info[0] >= 3:
# Open the file in text mode with given encoding
# Set newline arg to '' (see https://docs.python.org/3/library/csv.html)
with open(filename, 'r', newline='', encoding=encoding) as f:
generator = read_unicode_csv_fileobj(f, delimiter=delimiter,
quotechar=quotechar,
quoting=quoting,
lineterminator=lineterminator,
encoding=encoding,
skiprows=skiprows)
for row in generator:
yield row
# Python 2 version
else:
# Open the file in binary mode
with open(filename, 'rb') as f:
generator = read_unicode_csv_fileobj(f, delimiter=delimiter,
quotechar=quotechar,
quoting=quoting,
lineterminator=lineterminator,
encoding=encoding,
skiprows=skiprows)
for row in generator:
yield row