Connecting Tech Pros Worldwide Forums | Help | Site Map

a simple string question

vedrandekovic@v-programs.com
Guest
 
Posts: n/a
#1: Jul 27 '07
Hello,

I have one question about string.I am trying to make an function to
analyze line of some text, this is my example: "HELLO;HELLO2:WORLD:",
if that function in this text find ";" and ":" ( in this example will
find both)

e.g that function must return this:


"HELLO;\nHELLO2:\n\t\t\t\t\t\t\tWORLD:"




Regards,
Vedran


Zentrader
Guest
 
Posts: n/a
#2: Jul 27 '07

re: a simple string question


On Jul 27, 8:23 am, vedrandeko...@v-programs.com wrote:
Quote:
Hello,
>
I have one question about string.I am trying to make an function to
analyze line of some text, this is my example: "HELLO;HELLO2:WORLD:",
if that function in this text find ";" and ":" ( in this example will
find both)
>
e.g that function must return this:
>
"HELLO;\nHELLO2:\n\t\t\t\t\t\t\tWORLD:"
>
Regards,
Vedran
You can use split twice
print text.split( ":" )
and then split the returned list items on ";".
You could also use text.find( ":" ), but that would be pretty much the
same thing only harder. Also, you can step through the string one
character at a time and compare each character. Finally, you can use
an re, (regular expression), but if you are still learning how to
parse strings, I don't think you want to get into re's.

Wildemar Wildenburger
Guest
 
Posts: n/a
#3: Jul 27 '07

re: a simple string question


vedrandekovic@v-programs.com wrote:
Quote:
I have one question about string.I am trying to make an function to
analyze line of some text, this is my example: "HELLO;HELLO2:WORLD:",
if that function in this text find ";" and ":" ( in this example will
find both)
>
e.g that function must return this:
>
>
"HELLO;\nHELLO2:\n\t\t\t\t\t\t\tWORLD:"
>
If I understand you correctly you want to replace ";" by ";\n" and ":"
by ":\n\t\t\t\t\t\t\t".
Well guess what? The replace() method does just this. Have a read:
<URL:http://docs.python.org/lib/string-methods.html>

/W
vedrandekovic@v-programs.com
Guest
 
Posts: n/a
#4: Jul 27 '07

re: a simple string question


On 27 srp, 19:29, Wildemar Wildenburger <wilde...@freakmail.dewrote:
Quote:
vedrandeko...@v-programs.com wrote:
Quote:
I have one question about string.I am trying to make an function to
analyze line of some text, this is my example: "HELLO;HELLO2:WORLD:",
if that function in this text find ";" and ":" ( in this example will
find both)
>
Quote:
e.g that function must return this:
>
Quote:
"HELLO;\nHELLO2:\n\t\t\t\t\t\t\tWORLD:"
>
If I understand you correctly you want to replace ";" by ";\n" and ":"
by ":\n\t\t\t\t\t\t\t".
Well guess what? The replace() method does just this. Have a read:
<URL:http://docs.python.org/lib/string-methods.html>
>
/W
Hello,

No,that's not what I need...
When this function detect ";" or ":" ,it must append character "\n" or
"\n\t" ahead ":" or ";" another e.g

1) text="Hello world;Hello:Hello2"

2) When function detect ";" or ":" it must append character "\n" or "\n
\t" ahead ":" or ";", so that must look like this:

NEW TEXT : "Hello world;\nHello:\n\t\t\n\n\n\n\n\nHello2"

Regards,
Vedran

Wildemar Wildenburger
Guest
 
Posts: n/a
#5: Jul 27 '07

re: a simple string question


vedrandekovic@v-programs.com wrote:
Quote:
Quote:
>If I understand you correctly you want to replace ";" by ";\n" and ":"
>by ":\n\t\t\t\t\t\t\t".
>Well guess what? The replace() method does just this. Have a read:
><URL:http://docs.python.org/lib/string-methods.html>
>>
No,that's not what I need...
When this function detect ";" or ":" ,it must append character "\n" or
"\n\t" ahead ":" or ";" another e.g
>
1) text="Hello world;Hello:Hello2"
>
2) When function detect ";" or ":" it must append character "\n" or "\n
\t" ahead ":" or ";", so that must look like this:
>
NEW TEXT : "Hello world;\nHello:\n\t\t\n\n\n\n\n\nHello2"
>
>
Isn't that what I said?

Please note that appending "\n" to ";" is the very same thing as
replacing ";" with ";\n".

Also note that the your description of the desired behavior does not
match your example. You say "append "\n\t" after ":"", but thats not
what happens in your example. There you append "\n\t\t\n\n\n\n\n\n"
instead of "\n\t". That is confusing to me. Can you explain?

/W
Zentrader
Guest
 
Posts: n/a
#6: Jul 28 '07

re: a simple string question


On Jul 27, 11:26 am, Wildemar Wildenburger <wilde...@freakmail.de>
wrote:
Quote:
vedrandeko...@v-programs.com wrote:
Quote:
Quote:
If I understand you correctly you want to replace ";" by ";\n" and ":"
by ":\n\t\t\t\t\t\t\t".
Well guess what? The replace() method does just this. Have a read:
<URL:http://docs.python.org/lib/string-methods.html>
>
Quote:
No,that's not what I need...
When this function detect ";" or ":" ,it must append character "\n" or
"\n\t" ahead ":" or ";" another e.g
>
Quote:
1) text="Hello world;Hello:Hello2"
>
Quote:
2) When function detect ";" or ":" it must append character "\n" or "\n
\t" ahead ":" or ";", so that must look like this:
>
Quote:
NEW TEXT : "Hello world;\nHello:\n\t\t\n\n\n\n\n\nHello2"
>
Isn't that what I said?
>
Please note that appending "\n" to ";" is the very same thing as
replacing ";" with ";\n".
>
Also note that the your description of the desired behavior does not
match your example. You say "append "\n\t" after ":"", but thats not
what happens in your example. There you append "\n\t\t\n\n\n\n\n\n"
instead of "\n\t". That is confusing to me. Can you explain?
>
/W
Confusing to me also. I read this as replace the first ";" with ";/
n", replace the first ":" with "\n\t\t\t\t\t\t\t", and on from there
with different requirements. If that is the case then you have to
locate the first, second, etc. and add/replace with the appropriate
code. String.find and split would have to be used instead of a
string.replace if you want to make different changes depending on if
it is the first, second..., occurrence.

Zentrader
Guest
 
Posts: n/a
#7: Jul 28 '07

re: a simple string question


vedrandeko...@v-programs.com wrote:
Quote:
Quote:
Quote:
NEW TEXT : "Hello world;\nHello:\n\t\t\n\n\n\n\n\nHello2"
If you are doing all of this to format the output into columns,
Python's print() or write() will do this, and is easier as well. Some
more info on what you want to do will clear things up.

vedrandekovic@v-programs.com
Guest
 
Posts: n/a
#8: Jul 28 '07

re: a simple string question


On 28 srp, 07:05, Zentrader <zentrad...@gmail.comwrote:
Quote:
Quote:
Quote:
vedrandeko...@v-programs.com wrote:
NEW TEXT : "Hello world;\nHello:\n\t\t\n\n\n\n\n\nHello2"
>
If you are doing all of this to format the output into columns,
Python's print() or write() will do this, and is easier as well. Some
more info on what you want to do will clear things up.
Hi,

That is confusing me too, so now I will try explain it more.This is
text before "translation":
Let me explain you with python code. I want to this "function" act
code indentation
Quote:
Quote:
Quote:
>>Short_Text="n=90; if n==90:print 'ok'"
Then now I must write that function for detect ";" and ":", and if
that function detect ";" then it appends "\n" before ";" but
if detect ":" then it appends "\n\t\t\t\t\t\t\t\t"
Quote:
Quote:
Quote:
>>Short_text_after_translation="n=90;\nif n==90:\n\t\t\t\t\t\t\t\tprint 'ok"
....And now when we run this code with exec this must look like:

n=90;
if n==90:
print 'ok'

I think this will be enough for help.

Regards,
Vedran

Steve Holden
Guest
 
Posts: n/a
#9: Jul 28 '07

re: a simple string question


vedrandekovic@v-programs.com wrote:
Quote:
On 28 srp, 07:05, Zentrader <zentrad...@gmail.comwrote:
Quote:
Quote:
>>>vedrandeko...@v-programs.com wrote:
>>>>NEW TEXT : "Hello world;\nHello:\n\t\t\n\n\n\n\n\nHello2"
>If you are doing all of this to format the output into columns,
>Python's print() or write() will do this, and is easier as well. Some
>more info on what you want to do will clear things up.
>
Hi,
>
That is confusing me too, so now I will try explain it more.This is
text before "translation":
Let me explain you with python code. I want to this "function" act
code indentation
>
Quote:
Quote:
>>>Short_Text="n=90; if n==90:print 'ok'"
Then now I must write that function for detect ";" and ":", and if
that function detect ";" then it appends "\n" before ";" but
if detect ":" then it appends "\n\t\t\t\t\t\t\t\t"
Quote:
Quote:
>>>Short_text_after_translation="n=90;\nif n==90:\n\t\t\t\t\t\t\t\tprint 'ok"
...And now when we run this code with exec this must look like:
>
n=90;
if n==90:
print 'ok'
>
I think this will be enough for help.
>
OK, but you don't want that many tab characters if you can the code to
look like you show it. It's not, anyway, a good idea to use tabs to
indent code.

I suspect what you need is to split the code on semicolons first, then
re-form lines with colons in them. Some simple code to do this would
look *something* like what follows. This will handle a little more than
you wanted.
Quote:
Quote:
Quote:
>>Short_Text="n=90; if n==90:print 'ok'"
>>compound_lines = Short_Text.split(";")
>>for line in compound_lines:
.... line = line.replace(":", ":\n ")
.... print line
....
n=90
if n==90:
print 'ok'
Quote:
Quote:
Quote:
>>>
Note there are issues here that I haven't addressed. The first is that
leading spaces on the second statement need to be removed, and the
second is that this only works at the outermost level of indentation.
For example, if you want to end up translating function definitions with
if statements inside them correctly you will need to handle multiple
levels of indentation. There are other problems, like semicolons and
colons inside string constants should be ignored, but the only way to
get over those will be to parse the program text according to some
grammar rather than using ad-hoc methods such as the above.

I hope I have finally been of some assistance ... please reply via the
newsgroup, not in personal email.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------

Zentrader
Guest
 
Posts: n/a
#10: Jul 28 '07

re: a simple string question


>>Short_Text="n=90; if n==90:print 'ok'"
Quote:
Quote:
Quote:
>>compound_lines = Short_Text.split(";")
>>for line in compound_lines:
... line = line.replace(":", ":\n ")
... print line
...
n=90
if n==90:
print 'ok'
A variation of this will work if the input file isn't too
complicated. I found this link with a Google of "c to python". This
will give you an idea of how difficult it can be if you take into
account every possibility when converting. You might check the web
first, since someone has probably already done what you want. Good
luck.
http://www.catb.org/~esr/ctopy/

Closed Thread


Similar Python bytes