Skip to Content

python code convert

  • strict warning: Non-static method view::load() should not be called statically in /var/www/drupal_module/views/views.module on line 906.
  • strict warning: Declaration of views_handler_filter::options_validate() should be compatible with views_handler::options_validate($form, &$form_state) in /var/www/drupal_module/views/handlers/views_handler_filter.inc on line 0.
  • strict warning: Declaration of views_handler_filter::options_submit() should be compatible with views_handler::options_submit($form, &$form_state) in /var/www/drupal_module/views/handlers/views_handler_filter.inc on line 0.
  • strict warning: Declaration of views_handler_filter_node_status::operator_form() should be compatible with views_handler_filter::operator_form(&$form, &$form_state) in /var/www/drupal_module/views/modules/node/views_handler_filter_node_status.inc on line 0.
  • strict warning: Declaration of views_plugin_row::options_validate() should be compatible with views_plugin::options_validate(&$form, &$form_state) in /var/www/drupal_module/views/plugins/views_plugin_row.inc on line 0.
  • strict warning: Declaration of views_plugin_row::options_submit() should be compatible with views_plugin::options_submit(&$form, &$form_state) in /var/www/drupal_module/views/plugins/views_plugin_row.inc on line 0.
  • strict warning: Non-static method view::load() should not be called statically in /var/www/drupal_module/views/views.module on line 906.
  • strict warning: Non-static method view::load() should not be called statically in /var/www/drupal_module/views/views.module on line 906.
  • strict warning: Non-static method view::load() should not be called statically in /var/www/drupal_module/views/views.module on line 906.
  • strict warning: Declaration of views_handler_field_comment::init() should be compatible with views_handler_field::init(&$view, $options) in /var/www/drupal_module/views/modules/comment/views_handler_field_comment.inc on line 0.
  • strict warning: Declaration of views_handler_filter_boolean_operator::value_validate() should be compatible with views_handler_filter::value_validate($form, &$form_state) in /var/www/drupal_module/views/handlers/views_handler_filter_boolean_operator.inc on line 0.

常见的编码转换分为以下几种情况:

  • unicode->其它编码

    例如:a为unicode编码 要转为gb2312。a.encode('gb2312')
  • 其它编码->unicode

    例如:a为gb2312编码,要转为unicode. unicode(a, 'gb2312')或a.decode('gb2312')
  • 编码1 -> 编码2

    可以先转为unicode再转为编码2

    如gb2312转big5
    unicode(a, 'gb2312').encode('big5')
  • 判断字符串的编码

    isinstance(s, str) 用来判断是否为一般字符串
    isinstance(s, unicode) 用来判断是否为unicode
  • 如果一个字符串已经是unicode了,再执行unicode转换有时会出错(并不都出错)

    可以写一个通用的转成unicode函数:
    def u(s, encoding):
        if isinstance(s, unicode):
            return s
        else:
            return unicode(s, encoding)

那么unicode与utf-8,utf-16有什么区别呢。我认为unicode可以称为抽象编码,也就是它只是一种内部表示,一般不能直接保存。保存到磁盘上时,需要把它转换为对应的编码,如utf-8和utf-16。

除上以上的编码方法,在读写文件时还可以使用codecs的open方法在读写时进行转换。

-- EOF-TRUEVUE --