English 中文(简体)
灰色翻译,如何替换一个字
原标题:Python translator, how to replace just one word

因此,i m试图在Python(在60个装置中)创建一名翻译。 因此,试图做的是,在不触及其他词的情况下仅用一个字。 这里的例子

Original: "The brown fox jumps over the dog named brownie." I want to replace the word "brown" into "deathlesi"(Just ignore why) The result should be: "The deathlesi fox jumps over the dog named brownie." But instead it also changes "brownie" in the string which results to: "The deathlesi fox jumps over the dog named deathlesiie."

Since I m trying to replace each and every word, sometimes it goes into a never ending paradox. Example: "I am stupid" I m trying to change "I" into "ium" and this is what happens. "iumumumumumumumumumumumumumumumumumumumum.... am stupiuiuiuiuiuiuiuiuiuiuiuiuiuiuiuiuiuiuim..", it basically changes every "I" in the string and won t stop until there s no "I" in the string.

任何帮助? 感谢!

Edit:我已经尝试过“地圈”,但某些部分,如“一”小块,通常取代“一” st。

Here s another example: "People are getting excited at the giant hare." replacing "are" to "iume", instead of "People iume getting excited at the giant hare." it also replaced "hare" which resulted into "People iume getting excited at the giant hiume."

Supposedly I array ed the sentence and translate each of them. That is my current method now. Basically converting each word into a array and converting each one of them. Then doing a

translated_sentence=["particulus:people", "iume:are", "geus:getting", "exchantus:excited", "d:at", "qun:the", "gesas:giant", "hsont:hare"]
sentence= "People are getting excited at the giant hare."
for i in translated_sentence do
element=i.split(":")
sentence=sentence.replace(element[1], element[0])

并且仍然投下“部分ulu”

I just got it figured it out. I just splitted the string into an array, and preserved the formatting by cleaning the current word and doing a string.replace() to the original word.

sentence="The quick brown fox jumps over the lazy dog.".split(" ")
result=""

for i in sentence:

cleaned=clean(i) #removes the punctuations and stuff leaving the raw word.

translated=translate(cleaned) #returns the translated word

result=result+i.replace(cleaned,translated)+" "

return result
最佳回答

既然你只想发现第一起事件,那么你就需要一种办法来追踪。 你们可以采取许多办法。 简单明了:

def replacer(original, looking_for, replace_with):
       A straightforward way...    
   return original.replace(looking_for, replace_with, 1)
   #return regex.sub(replace_with, looking_for, 1)

这些数字表明,你想要取代多少次。 如果存在两种情况,则将取代这两种情况。

努力是不可调和的,因此,你必须重新签署新的指示。 每当你填写<代码>replace时,你都会产生新的指示。

You can also write a loop to find the N-th occurrence if you don t want the built-in.

I recommend making your post shorter (I mean fewer words, and more syntax highlight). Format it. Correct me if I didn t read your post correctly.

问题回答

like like:

import re
x = "The brown fox jumps over the dog named brownie."
newstring = re.sub(r"(s+|[:punct:]+|^)brown(s+|[:punct:]+|$)",r"1deathlies2",x, flags=re.IGNORECASE)

产量:

>>> print newstring
The deathlies fox jumps over the dog named brownie.

或:

x = "People are getting excited at the giant hare."
newstring = re.sub(r"(s+|[:punct:]+|^)are(s+|[:punct:]+|$)",r"1iume2",x, flags=re.IGNORECASE)

哪些要点:

>>> print newstring
People iume getting excited at the giant hare.

第一组:<代码>(s+>:punct:>+><>>>>>>> /代码对空间、校正或方位表和另一组<代码>(s+组:punct]+$>>

在进行替换时,<代码>1和2将 p或间隔与替代案文相重,使事情变得毫无意义。

PS

如果您重读,就只将捕获组编成<条码>(W+|)(W+$)。

仅打电话替代加强职能

"I am stupid".replace("I", "ium")

我现在不听我的话,但是,如何发挥职能,将扼杀变成一个名单。 你们可以走白色空间,因此名单是[own、英、 jump......]。 之后是。

你们想要取代确切的同词。 不是扼杀。 替换

取代“区域”但并不取代“收获”

如果情况如此,

edited

http://docs.python.org/howto/regex.html#search-and-replace” rel=“nofollow” 定期搜索和替换是你完成任务的最佳工具。

或者,如果你刚刚开始学习Python和Regex,就过于简单化。 仅用str.split()说几句话。

def simply_replace(string, search, replace):
    words = string.split(   )
    for i in range(len(words)):
        if(words[i].lower() == search):
            words[i] = replace
    return    .join(words)

>>> simply_replace("I am stupid",  i ,  ium )
 ium am stupid 
>>> simply_replace("The brown fox jumps over the dog named brownie.",  brown ,  deathly )
 The deathly fox jumps over the dog named brownie. 
>>> simply_replace("People are getting excited at the giant hare.",  are ,  ium )
 People ium getting excited at the giant hare. 




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签