小编use*_*864的帖子

在Python中使用file.write写入文件时出错.UnicodeEncodeError

我从来没有处理过编码和解码字符串,所以我就是这方面的新手.当我尝试使用Python中的file.write将我从另一个文件读取的内容写入临时文件时,我收到了一个UnicodeEncodeError.我收到以下错误:

UnicodeEncodeError: 'ascii' codec can't encode character u'\u201c' in position 41333: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)

这是我在我的代码中所做的.我正在读取XML文件并从"mydata"标签获取文本.然后我遍历mydata寻找CDATA

    parser = etree.XMLParser(strip_cdata=False)
    root = etree.parse(myfile.xml, parser)
    data = root.findall('./mydata')
    # iterate through list to find text (lua code) contained in elements containing CDATA
    for item in myData:
        myCode = item.text

    # Write myCode to a temporary file.
    tempDirectory = tempfile.mkdtemp(suffix="", prefix="TEST_THIS_")
    file = open(tempDirectory + os.path.sep + "myCode.lua", "w")

    file.write(myCode + "\n")
    file.close()
Run Code Online (Sandbox Code Playgroud)

当我点击以下行时,它失败了UnicodeEncodeError:

file.write(myCode + "\n")
Run Code Online (Sandbox Code Playgroud)

我应该如何正确编码和解码?

unicode encode decode fwrite python-2.7

11
推荐指数
1
解决办法
9659
查看次数

如何让py2exe构建版权信息

我正在使用Py2exe从我的Python脚本创建一个Windows .exe.我想拥有版权信息以及产品版本,描述等.我已经能够显示所有内容(在属性> exe的详细信息中),版权信息除外.我试过以下但没有成功:

from distutils.core import setup
import py2exe
import sys

if len(sys.argv) == 1:
    sys.argv.append("py2exe")
    sys.argv.append("-q")

class Target:
    def __init__(self, **kw):
        self.__dict__.update(kw)
        # for the versioninfo resources
        self.version = "1.0.0.0"
        self.company_name = "ACME."
        self.copyright = "Copyright (c) 2014 ACME."
        self.name = "My Program"

# create an instance of class Target
# and give it additional needed info
target = Target(
    description = "Test Description",
    # this is your code file
    script = "Main.py",
    # this will form TestProgram.exe
    dest_base …
Run Code Online (Sandbox Code Playgroud)

python windows exe py2exe

7
推荐指数
2
解决办法
2937
查看次数

如何使用Python验证特定的日期和时间格式

我正在编写一个程序来验证XML文件的各个部分.我想验证的一点是日期时间格式.我已经在论坛上阅读了有关使用的内容,time.strptime()但这些示例对我来说并不是很有效,而且有点超过我的专业知识.任何人都有任何想法如何验证以下内容.这是日期和时间必须的格式.

2/26/2009 3:00 PM
Run Code Online (Sandbox Code Playgroud)

我确信有一些内置的东西很容易,但我找不到.非常感谢你以前经营过这个并提出建议.

python format datetime

5
推荐指数
1
解决办法
2万
查看次数

如何在回显字符串时保持PHP不受strippng XML标记的影响

我正在通过exec在PHP中执行外部命令,我接受该输出(这是一个数组),然后让各个字符串搜索特定的字符串.然后我希望将这些字符串回显到屏幕上.但是,其中一些字符串包含XML示例,并且它们正在被剥离.如何防止PHP剥离XML?我使用的是PHP 5.6.2.

例如,我试图回显具有以下输出的$ val:

Missing test tag.  Please add the test tag and set it to true.  i.e. <data><test>true</test></data>.
Run Code Online (Sandbox Code Playgroud)

但相反,我得到以下内容:

Missing test tag. Please add the test tag and set it to true. i.e. true.
Run Code Online (Sandbox Code Playgroud)

如您所见,"数据"和"测试"xml标签正在被剥离.

php xml php-5.6

2
推荐指数
1
解决办法
636
查看次数

Python中的正则表达式帮助.寻找RGB值的表达式

看起来我需要一些正则表达式的帮助来匹配RGB值.我已经构建了以下表达式但它找不到匹配项.它也不够,因为我需要扩展它以检查三位数,并且只存在0-255值.

例如.以下是我需要匹配的所有有效值:

0,0,0
0, 0, 0
255,255,255
255, 255, 255
Run Code Online (Sandbox Code Playgroud)

这是我到目前为止:

expression = re.compile(r'(\\d+),(,),(\\d+),(,),(\\d+)')
expression.match(text)
Run Code Online (Sandbox Code Playgroud)

python regex rgb

0
推荐指数
1
解决办法
402
查看次数

标签 统计

python ×3

datetime ×1

decode ×1

encode ×1

exe ×1

format ×1

fwrite ×1

php ×1

php-5.6 ×1

py2exe ×1

python-2.7 ×1

regex ×1

rgb ×1

unicode ×1

windows ×1

xml ×1