繁体   English   中英

使用docx在Word文档上打印度数符号的编码问题

[英]Encoding issue with printing the degree symbol on a word document using docx

尝试使用python docx将度数符号添加到word文档中,而我的函数定义如下:

def convert_decimal_degrees2DMS(self,value):
    #value = math.fabs(value)
    degrees = int(value)
    submin = math.fabs( (value - int(value) ) * 60)
    minutes = int(submin)
    subseconds = round(math.fabs((submin-int(submin)) * 60),1)
    subseconds = int(subseconds)
    self.angle = str(degrees) + " Degrees " + str(minutes) + " Minutes " +\
               str(subseconds)[0:2] + " Seconds "
    #self.angle = str(degrees) + "-" + str(minutes) + "-" + str(subseconds)
    #return str(degrees) + "-" + str(minutes) + "-" + str(subseconds)
    #degree = u'\N{DEGREE SIGN}'.encode('utf-8')
    return "{0}{1}{2}'{3}''".format(degrees,u'°'.encode('cp1252'),minutes,subseconds)

我不断得到的错误是:

  File "lxml.etree.pyx", line 921, in lxml.etree._Element.text.__set__ (src\lxml\lxml.etree.c:41467)
  File "apihelpers.pxi", line 652, in lxml.etree._setNodeText (src\lxml\lxml.etree.c:18888)
  File "apihelpers.pxi", line 1335, in lxml.etree._utf8 (src\lxml\lxml.etree.c:24701)
ValueError: All strings must be XML compatible: Unicode or ASCII, no NULL bytes or control characters
Exception AttributeError: "'NoneType' object has no attribute 'print_exc'" in <function _remove at 0x01E0F770> ignored

我已经尝试了许多变体,但没有任何效果,我担心这可能是由于我对编码缺乏理解而导致的。

u'°'.encode('cp1252')返回一个等效于'\\xb0' u'°'.encode('cp1252')的字节字符串(类型为str )。 同样,在其他地方,您正在将事物转换为str 该错误告诉您需要unicode (Unicode代码点)类型的字符串,而不是str (字节)。 度数符号本身可能不是问题。

解决方案是简单地提供Unicode字符串:所以用u'°'代替u'°'.encode('cp1252') ,以及

self.angle = degrees + u" Degrees " + minutes + u" Minutes " + \
               subseconds[0:2] + u" Seconds "

代替

self.angle = str(degrees) + " Degrees " + str(minutes) + " Minutes " +\
               str(subseconds)[0:2] + " Seconds "

(假设degrees等类型为unicode而不是str )。 请注意,Unicode字符串的u''语法与字节字符串的''语法相反。

关于Python源中包含非ASCII字符,您要记住的一件事是PEP-0263中记录的编码标头。 因此,您可以在shebang后面加上一个编码声明:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

请记住,使用PEP 0263不会神奇地消除strunicode的对偶性。 '°'将是在磁盘上的源代码文件中找到的任意str (字节字符串),因此长度不一定为1(如果是ISO-8859-1,则相当于'\\xb0' xb0 '\\xb0' ;如果是DOS cp437,则等于'\\xb0' '\\xf8' (如果是UTF-8,则为'\\xc2\\xb0' )。 u'°'将是Unicode代码点U+00B0而不管源代码的编码如何。

这是源代码中非ASCII字符的说明。 对于此示例,查看源代码的实际字节很重要。 源代码是UTF-8编码的,因此'°'长度为2; 它毕竟是一个字节字符串。

$ cat x.py 
#!/usr/bin/python
# -*- coding: UTF-8 -*-

print repr('°')
print len('°')
print len(u'°')

$ od -c -txC x.py
0000000    #   !   /   u   s   r   /   b   i   n   /   p   y   t   h   o
           23  21  2f  75  73  72  2f  62  69  6e  2f  70  79  74  68  6f
0000020    n  \n   #       -   *   -       c   o   d   i   n   g   :    
           6e  0a  23  20  2d  2a  2d  20  63  6f  64  69  6e  67  3a  20
0000040    U   T   F   -   8       -   *   -  \n  \n   p   r   i   n   t
           55  54  46  2d  38  20  2d  2a  2d  0a  0a  70  72  69  6e  74
0000060        r   e   p   r   (   '   °  **   '   )  \n   p   r   i   n
           20  72  65  70  72  28  27  c2  b0  27  29  0a  70  72  69  6e
0000100    t       l   e   n   (   '   °  **   '   )  \n   p   r   i   n
           74  20  6c  65  6e  28  27  c2  b0  27  29  0a  70  72  69  6e
0000120    t       l   e   n   (   u   '   °  **   '   )  \n            
           74  20  6c  65  6e  28  75  27  c2  b0  27  29  0a
$ python x.py
'\xc2\xb0'
2
1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM