本文整理汇总了Python中utils.Utils.unicode_csv_reader方法的典型用法代码示例。如果您正苦于以下问题:Python Utils.unicode_csv_reader方法的具体用法?Python Utils.unicode_csv_reader怎么用?Python Utils.unicode_csv_reader使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类utils.Utils
的用法示例。
在下文中一共展示了Utils.unicode_csv_reader方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: import_session_templates
# 需要导入模块: from utils import Utils [as 别名]
# 或者: from utils.Utils import unicode_csv_reader [as 别名]
def import_session_templates(self, auth_token, csv_object, interactive=False):
"""
Common method to import session_templates from csv data
@param csv_object A csv_data object. Every field should be in double quotes.
@return A list of primary keys for the newly created session_templates
"""
# These are the fields that are required in order to import a SessionTemplate object
required_session_template_fields = ['shortname', 'fullname', 'version', 'description', 'price', 'lead_time', 'active']
# the True value makes it leave the \n character on the ends of lines
lines = csv_object.text.splitlines(True)
reader = Utils.unicode_csv_reader(lines)
keys = [] # Store primary keys of newly created session_templates here
line_num = 1 # keep track of which line we're on
exception_message = '' # If we get exceptions, we just add them to this string and keep going, so we can report all the problems at the end
for row in reader:
line_num += 1
self._form_create_dict(auth_token, row, required_session_template_fields)
try:
c = self.session_template_manager._create(**row)
keys.append(c.id)
except ValueError, ve:
if len(exception_message):
exception_message += '\n'
exception_message += 'line ' + str(line_num) + ': ValueError:' + str(ve)
except exceptions.InvalidDataException, ide:
if len(exception_message):
exception_message += '\n'
exception_message += 'line ' + str(line_num) + ': ' + str(ide)
示例2: import_regions
# 需要导入模块: from utils import Utils [as 别名]
# 或者: from utils.Utils import unicode_csv_reader [as 别名]
def import_regions(self, auth_token, csv_data, interactive=False):
"""
Import regions from CSV data.
@param auth_token
@type auth_token models.AuthToken
@param csv_data A csv_data object.
@type csv_data models.csv
@return A list of primary keys for the newly created regions
@rtype list of int
"""
required_fields = ['name']
return self._import_from_csv(auth_token, csv_data, self.region_manager,
required_fields, interactive=interactive)
csv_object = self._upload_security_common(auth_token, csv_data_id)
# the True value makes it leave the \n character on the ends of lines
lines = csv_object.text.splitlines(True)
csv_object = None # Garbage collection?
reader = Utils.unicode_csv_reader(lines)
keys = [] # Store primary keys of newly created venues here
line_num = 1
#: cache regions from the database
region_cache = {}
for row in reader:
name = row[0]
r = self.region_manager.create(auth_token, name)
keys.append(r.id)
line_num += 1
return keys
示例3: import_rooms
# 需要导入模块: from utils import Utils [as 别名]
# 或者: from utils.Utils import unicode_csv_reader [as 别名]
def import_rooms(self, auth_token, csv_data, interactive=False):
"""
Common method for importing venues from csv data
@param csv_data A foreign key for a csv_data object as returned from a POST
request to upload_csv/. Every field should be in double quotes.
Fields in order are: name, venue (foreign key), capacity
@return A list of primary keys for the newly created venues
"""
csv_object = self._upload_security_common(auth_token, csv_data)
# the True value makes it leave the \n character on the ends of lines
lines = csv_object.text.splitlines(True)
csv_object = None # Garbage collection?
reader = Utils.unicode_csv_reader(lines)
keys = [] # Store primary keys of newly created venues here
line_num = 0
for row in reader:
try:
v = self.room_manager.create(auth_token, row[0], row[1], row[2])
except ValueError, ve:
raise exceptions.InvalidDataException('line ' + str(line_num) +
': ValueError:' + str(ve))
except exceptions.InvalidDataException, ide:
raise exceptions.InvalidDataException('line ' + str(line_num) +
': ' + str(ide))
示例4: import_sessions
# 需要导入模块: from utils import Utils [as 别名]
# 或者: from utils.Utils import unicode_csv_reader [as 别名]
def import_sessions(self, auth_token, csv_data, interactive=False):
"""
Common method for importing sessions from csv data
@param csv_data A csv_data object. Every field should be in double quotes.
@return A list of primary keys for the newly created Sessions
"""
# These are the fields that are required in order to import a Session object
required_session_fields = ['start', 'end', 'status', 'confirmed', 'default_price', 'event']
required_event_fields = ['name_prefix', 'title', 'description', 'start', 'end', 'organization', 'product_line']
# the True value makes it leave the \n character on the ends of lines
lines = csv_data.text.splitlines(True)
csv_object = None # Garbage collection?
reader = Utils.unicode_csv_reader(lines)
keys = {} # Store primary keys of newly created sessions and events here
keys['sessions'] = []
keys['events'] = []
line_num = 1
events_created = {} # Store primary keys of the events as they are created, indexed by the 'unique_identifier'
exception_message = '' # If we get exceptions, we just add them to this string and keep going, so we can report all the problems at the end
for row in reader:
line_num += 1
event_fields = self._get_model_fields(row, 'event')
unique_event_identifier = event_fields.pop('unique_identifier')
if unique_event_identifier not in events_created:
# Create the event
self._form_create_dict(auth_token, event_fields, required_event_fields)
try:
new_event = self.event_manager.create(**event_fields)
keys['events'].append(new_event.id)
events_created[unique_event_identifier] = new_event.id
except ValueError, ve:
if len(exception_message):
exception_message += '\n'
exception_message += 'line ' + str(line_num) + ': ValueError:' + str(ve)
break
except exceptions.InvalidDataException, ide:
if len(exception_message):
exception_message += '\n'
exception_message += 'line ' + str(line_num) + ': ' + str(ide)
break
except facade.models.ModelDataValidationError, ve:
if len(exception_message):
exception_message += '\n'
exception_message += 'line ' + str(line_num) + ': ValidationError: ' + str(ve)
break
示例5: _import_from_csv
# 需要导入模块: from utils import Utils [as 别名]
# 或者: from utils.Utils import unicode_csv_reader [as 别名]
def _import_from_csv(self, auth_token, csv_data, manager, required_fields,
custom_row_function=None, special_fields=None, interactive=False):
"""
Generic method to import CSV data and run it through the create() method
of a manager.
@param csv_data A csv_data object
@type csv_data models.csv
@param manager Instance of manager that corresponds to the
object being created
@type manager ObjectManager
@param required_fields list of field names which are required by
the create() method
@type required_fields list
@param custom_row_function optional function that takes a row
dictionary as an argument and returns a row
dictionary. If you need to manipulate each
row in any way, this is the place to do it.
@type custom_row_function function
@param special_fields optional dictionary defining special fields
on the manager and how they should be
translated. This is particularly useful for
establishing many to many relationships in a
CSV.
@type special_fields dict
@return list of primary keys for newly created
objects
@rtype list of integers
"""
# the True value makes it leave the \n character on the ends of lines
lines = csv_data.text.splitlines(True)
csv_data = None # Garbage collection?
reader = Utils.unicode_csv_reader(lines)
keys = [] # Store primary keys of newly created venues here
line_num = 1
exception_details = {} # If we get exceptions, add a new entry to this
# dictionary for each line.
for row in reader:
line_num += 1
try:
if custom_row_function is not None:
row = custom_row_function(row)
self._form_create_dict(auth_token, row, required_fields,
special_fields)
v = manager.create(**row)
keys.append(v.id)
if interactive:
sys.stderr.write('.')
except ValueError, ve:
exception_details[line_num] = u'ValueError: %s' % unicode(ve)
if interactive:
sys.stderr.write('E')
print >> sys.stderr, "\nline %d: [%s]" % (line_num,
exception_details[line_num])
print >> sys.stderr, '\n', row, '\n'
except exceptions.InvalidDataException, ide:
exception_details[line_num] = unicode(ide)
if interactive:
sys.stderr.write('E')
print >> sys.stderr, "\nline %d: [%s]" % (line_num,
exception_details[line_num])
print >> sys.stderr, '\n', row, '\n'