English 中文(简体)
如何在沙里搜寻和替换古德八国特质?
原标题:How to search and replace utf-8 special characters in Python?

我是一把一只面包车开车,我有一只花八门的问题。

我有一张图八,我想用ASCII替代物来取代所有的高纬度(在德国,可以改写为座)。

u-umlaut有252个统一编码点,因此我尝试:

>>> str = unichr(252) +  ber 
>>> print repr(str)
u xfcber 
>>> print repr(str).replace(unichr(252),  ue )
u xfcber 

我期望最后一点是<代码>u ueber 。

我最终想要做的是,在档案中,用的是:

import sys
import codecs      
f = codecs.open(sys.argv[1],encoding= utf-8 )
for line in f: 
    print repr(line).replace(unichr(252),  ue )

感谢你们的帮助! (一百万人使用2.3)。

问题回答

我将界定一个具有特别特征的词典(我想绘制地图),然后我使用<代码>translate<>code>方法。

line =  Ich möchte die Qualität des Produkts überprüfen, bevor ich es kaufe. 

special_char_map = {ord( ä ): ae , ord( ü ): ue , ord( ö ): oe , ord( ß ): ss }
print(line.translate(special_char_map))

您将取得以下成果:

Ich moechte die Qualitaet des Produkts ueberpruefen, bevor ich es kaufe.

我认为,以更直截了当的方式,直接使用统一编码代表比不iichr(252)更好,是最容易和更清楚的。

>>> s = u über 
>>> s.replace(u ü ,  ue )
u ueber 

没有必要再行,因为这样会印古尔语的表述,你才需要提出可读的描述。

您还需要在案卷行乞时列入以下线,如果档案尚未到场,则要说明档案的编码。

#-*- coding: UTF-8 -*-

<><><> 当然,申报的编码必须与卷宗编码相同。 请核实问题(我对Windows的Eclipse存在问题,例如,由于文件在缺席时将文件作为p1252。) 该系统还应与系统相同的编码,即“ut”-8,或“ la-1或他人”。


此外,不使用<代码>str作为变数的定义,因为它是沙尔图书馆的一部分。 以后,你可能会有问题。

(我正试图粉碎2.6,我认为结果也是一样)

repr(str) 退回一个引文版str,在打印时,你可以打回波茨,以获得扼回。 因此,它载有一个字面上包含xfcber的插图,而不是包含über的插图。

您只能使用<代码>str.replace(unichr(252), ue ),以取代ü

如果您需要获得这一结果的引文,尽管我并不认为你需要,但你可以在<条码>repr上总结整个表述:

repr(str.replace(unichr(252),  ue ))

你们可以避免所有这种来源地表 st着 st子及其问题。 利用统法协会的名称,然后直截了当地清楚你正在做什么,法典可在任何地方读和修改。

我不知道哪一种语言是,唯一的拉丁字母缩略语是低地字母-u-with-umlaut-aka-diaeresis,因此,我补充说,根据你需要,在翻译桌旁摆着。

# coding: ascii

translations = (
    (u N{LATIN SMALL LETTER U WITH DIAERESIS} , u ue ),
    (u N{LATIN SMALL LETTER O WITH DIAERESIS} , u oe ),
    # et cetera
    )

test = u MN{LATIN SMALL LETTER O WITH DIAERESIS}ller von MN{LATIN SMALL LETTER U WITH DIAERESIS}nchen 

out = test
for from_str, to_str in translations:
    out = out.replace(from_str, to_str)
print out

产出:

Moeller von Muenchen




相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签