String search

P

pranav

Greeting fellow pycoders,

I have a script that browses large codes and replaces certain text
with some other text. Of lately i observed an issue.Some of the
original text were like

<cfif IsDefined("attributes.SOFTPREREQ")>
,N'#attributes.SOFTPREREQ#'
</cfif>

My module does scan this code and suggests replacement to this code.
But when i use the string.replace() method, it just fails. A
string.find() for above code in the whole file returns code -1.
Where am i going wrong? I figure it could be something to do with
character encoding or else.

Please someone guide me soon. I have a deadline to attend :-(

Thanks in advance,

Pranav
 
B

Bruno Desthuilliers

pranav a écrit :
Greeting fellow pycoders,

I have a script that browses large codes and replaces certain text
with some other text. Of lately i observed an issue.Some of the
original text were like

<cfif IsDefined("attributes.SOFTPREREQ")>
,N'#attributes.SOFTPREREQ#'
</cfif>

My module does scan this code and suggests replacement to this code.
But when i use the string.replace() method, it just fails. A
string.find() for above code in the whole file returns code -1.
Where am i going wrong?

Not posting a minimal (and if possible runnable) excerpt of your code
exhibiting the problem, along with test data.
I figure it could be something to do with
character encoding or else.

or with white spaces (including newlines characters), or whatever.

As a general guideline : any non-trivial text parsing requires a
non-trivial parser.
 
G

Gabriel Genellina

Greeting fellow pycoders,

I have a script that browses large codes and replaces certain text
with some other text. Of lately i observed an issue.Some of the
original text were like

<cfif IsDefined("attributes.SOFTPREREQ")>
,N'#attributes.SOFTPREREQ#'
</cfif>

My module does scan this code and suggests replacement to this code.
But when i use the string.replace() method, it just fails. A
string.find() for above code in the whole file returns code -1.
Where am i going wrong? I figure it could be something to do with
character encoding or else.

Do you mean, you look for those three complete lines with a single
xxxx.find(...)?
Hard to guess... maybe there is some withespace at the end of a line, some
additional leading whitespace in the second line, a tab character instead
of spaces, maybe those ' are instead ´ or `, maybe the file does not use
\n as a line termination, maybe the file encoding doesn't match what you
expect...

If string.find() does return -1, it is because the text you're looking for
is NOT inside the string. No use in questioning that. If you think the
text IS there, get a decent hex editor and examine the file near the
supposed match, character by character, and see WHERE is the difference
(there MUST be a difference).
 
O

odeits

Do you mean, you look for those three complete lines with a single  
xxxx.find(...)?
Hard to guess... maybe there is some withespace at the end of a line, some  
additional leading whitespace in the second line, a tab character instead  
of spaces, maybe those ' are instead ´ or `, maybe the file does not use  
\n as a line termination, maybe the file encoding doesn't match what you  
expect...

If string.find() does return -1, it is because the text you're looking for  
is NOT inside the string. No use in questioning that. If you think the  
text IS there, get a decent hex editor and examine the file near the  
supposed match, character by character, and see WHERE is the difference  
(there MUST be a difference).

Try looking into regular expressions.... http://docs.python.org/library/re.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top