【问题标题】:Convert namedtuple parameters to utf8将 namedtuple 参数转换为 utf8
【发布时间】:2018-06-05 18:24:01
【问题描述】:

目前,我正在从 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]

列表中的每个项目都作为命名元组处理。

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)

【问题讨论】:

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


    【解决方案1】:

    实现了这个:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-29
      • 1970-01-01
      • 2023-03-31
      相关资源
      最近更新 更多