小编gil*_*ipf的帖子

Python 2.7 UnicodeDecodeError:'ascii'编解码器无法解码字节

我一直在解析一些带有特殊字符(捷克语字母)的docx文件(UTF-8编码的XML).当我尝试输出到stdout时,一切顺利,但我无法将数据输出到文件,

回溯(最近一次调用最后一次):
文件"./test.py",第360行,
inile.write(u'\ t\t\t\t\t\t \n \n')
UnicodeEncodeError:'ascii'编解码器不能编码位置37的字符u'\ xed':序数不在范围内(128)

虽然我明确地将word变量转换为unicode类型(type(word)返回unicode),但我试图对它进行编码,.encode('utf-8)我仍然坚持这个错误.

以下是现在看到的代码示例:

for word in word_list:
    word = unicode(word)
    #...
    ofile.write(u'\t\t\t\t\t<feat att="writtenForm" val="'+word+u'"/>\n')
    #...
Run Code Online (Sandbox Code Playgroud)

我也尝试过以下方法:

for word in word_list:
    word = word.encode('utf-8')
    #...
    ofile.write(u'\t\t\t\t\t<feat att="writtenForm" val="'+word+u'"/>\n')
    #...
Run Code Online (Sandbox Code Playgroud)

即使是这两者的组合:

word = unicode(word)
word = word.encode('utf-8')
Run Code Online (Sandbox Code Playgroud)

我有点绝望,所以我甚至试图编码里面的单词变量 ofile.write()

ofile.write(u'\t\t\t\t\t<feat att="writtenForm" val="'+word.encode('utf-8')+u'"/>\n')
Run Code Online (Sandbox Code Playgroud)

我很欣赏任何我做错的提示.

python unicode

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

输出开始时的额外行

我应该编写一个类似于Unix tail函数的非常小的程序,我std::deque用来存储我std::getline从提供的文件中读取的行.从前面推,从后面弹出.

我的问题是,当我尝试打印更多行然后有文件时,它会在输出开始时输出1个额外的空白行.这里是源代码,TParamsstructint lncount存储要求的行数,其中,和其他一些在当前并不重要的东西...

using namespace std;

deque<string> dq;
int counter = 0;

for(string line; ! (*infile).eof(); getline(*infile, line)){
    dq.push_front(line);

    // not needed lines dropped immediately
    if(counter++ >= TParams.lncount)
        dq.pop_back();
}

int iter = (TParams.lncount > dq.size()) ?
           (dq.size() - 1) : (TParams.lncount - 1);

assert(iter < dq.size());

for(iter; iter >= 0; iter--)
    cout << dq[iter] << endl;
Run Code Online (Sandbox Code Playgroud)

有一些关于-n +num参数的代码,但它是内部条件,并不影响这种情况.

我发现,实际上存在零长度字符串,dq.back()但我完全不知道它来自哪里,因为它应该是从文件开头读取的字符串,但是有正常的文本行.

c++ io deque

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

传递函数指针的参数

我应该通过hashtable(指向数据链接列表的指针数组)和一些支持函数来编写C++ STL容器映射(关联数组)的C实现,即插入元素,删除表...我已经成功地编写了所有这些,除了一个foreach(table, function_ptr)函数,它调用表中所有数据的传递函数(打印内容......).

我有点卡在这里,因为我无法弄清楚应该传递什么参数function_ptr,所以它将是通用的.至于现在,我认为这是不可能的.

如果我只想将指针传递给printf,那将很容易,原型foreach看起来像这样

foreach(table_t *t, int (*function_ptr)(const char *fmt, ...))
Run Code Online (Sandbox Code Playgroud)

我会像这样为每个数据节点调用它

function_ptr("%s, %d\n", node.key, node.data)
Run Code Online (Sandbox Code Playgroud)

但如果我有一天使用它并改变我的想法,我想通过自己的功能,我将不得不改变调用函数和foreach函数的代码.

有没有简单的方法来做这样的事情?

c function-pointers hashtable

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

在python脚本中拥有漂亮的打印选项

我正在输出相当庞大的XML结构到文件,我希望用户能够启用/禁用漂亮的打印.

我正在使用大约150MB的数据,当我尝试xml.etree.ElementTree从它的元素对象构建树结构时,它使用了大量的内存,所以我通过存储原始字符串并输出来手动执行此操作.write().我的输出序列如下所示:

ofile.write(pretty_print(u'\
\t\t<LexicalEntry id="%s">\n\
\t\t\t<feat att="languageCode" val="cz"/>\n\
\t\t\t<Lemma>\n\
\t\t\t\t<FormRepresentation>\n\
\t\t\t\t\t<feat att="writtenForm" val="%s"/>\n\
\t\t\t\t</FormRepresentation>\n\
\t\t\t</Lemma>\n\
\t\t\t<Sense>%s\n' % (str(lex_id), word['word'], '' if word['pos']=='' else '\n\t\t\t\t<feat att="partOfSpeech" val="%s"/>' % word['pos'])))
Run Code Online (Sandbox Code Playgroud)

.write()我调用我的函数pretty_print 里面,根据命令行选项,应该删除所有制表符和换行符

o_parser = OptionParser()
# ....
o_parser.add_option("-p", "--prettyprint", action="store_true", dest="pprint", default=False)
# ....

def pretty_print(string):
    if not options.pprint:
        return string.strip('\n\t')
    return string
Run Code Online (Sandbox Code Playgroud)

我写了'should',因为它没有,在这种特殊情况下它不会删除任何字符.

但在这种情况下,它工作正常:

for ss in word['synsets']:
    ofile.write(pretty_print(u'\t\t\t\t<Sense synset="%s-synset"/>\n' % ss))
Run Code Online (Sandbox Code Playgroud)

我想到的第一件事是替换可能存在一些问题,但是当我在pretty_print 函数中打印传递的字符串时,它看起来非常好.

任何可能导致这种.strip()情况的建议都不起作用?
或者,如果有更好的方法,我会接受任何建议

python string pretty-print

-1
推荐指数
1
解决办法
280
查看次数

标签 统计

python ×2

c ×1

c++ ×1

deque ×1

function-pointers ×1

hashtable ×1

io ×1

pretty-print ×1

string ×1

unicode ×1