将namedtuple参数转换为utf8

时间:2018-06-05 18:24:02

标签: python python-2.7 character-encoding python-unicode

目前,我正在阅读Python 2.7中的REST API,然后处理JSON响应并将其存储为 namedtupled

结果以CSV文件写入。 目前我收到此错误:

UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 4: ordinal not in range(128)

示例API响应:

[u'private:bowlmorlanes', u'4983691', 30804108, u'20180604', u'Bowlmor AMF', 10753, 639.0, u'https://www.linkedin.com/company/4983691', u'Entertainment', u'20180604', u'20180604T155553Z', None, u'https://www.bowlmor.com', None, None]

列表中的每个项目都作为namedtupled处理。

Product = collections.namedtuple('Product', [
    'ticker_symbol', 'entity', 'unique_id', 'as_of_date', 'company_name',
    'followers', 'items', 'linkedin', 'industry', 'date_added',
    'date_updated', 'description', 'website', 'sector', 'ticker_industry'
])

处理回复:

  response_rows = response_items.get('rows')  
  # Request JSON string contains summary of request.
  results = []
  for response_row in response_rows:
    logging.info(response_row)
    results.append(Product(*response_row))
  return results

保存结果

def SaveDataSet(results: Sequence[Product], filename: str):
  """Writes results stored in Sequence into a file in CNS.

  Args:
    results: Results with app information.
    filename: Destination file.

  Raises:
    ValueError: Result list is empty.
    FileError: Unable to write filename.
  """
  if not results:
    raise ValueError('Result list is empty')
  logging.info('Saving results here: %s', filename)
  with open(filename, 'w+') as csvfile:
    filewriter = csv.writer(csvfile)
    filewriter.writerows(results)
  logging.info('URLs processed: %d.', len(results))


SaveDataSet(results, output_file)

问题: 使用此函数转换namedtuple中每个参数以防止错误的最佳方法是什么:

def _ToString(value):
  """Returns a string type based on value variable type.

  Since we handle multiple languages we need to return a string type to write
  in file human readable character.

  Args:
    value: (None, str or unicode)

  Returns:
    A str or None if no input.
  """

  if not value:
    logging.warning('Empty value')
    return None

  if isinstance(value, unicode):
    return value.encode('utf-8')
  else:
    return str(value)

1 个答案:

答案 0 :(得分:0)

实现了这个:

results.append(Product(*[_ToString(x) for x in response_row]))