471,326 Members | 2,042 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,326 software developers and data experts.

a simple string question

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

Jul 27 '07 #1
9 1065
On Jul 27, 8:23 am, vedrandeko...@v-programs.com wrote:
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.

Jul 27 '07 #2
ve***********@v-programs.com wrote:
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
Jul 27 '07 #3
On 27 srp, 19:29, Wildemar Wildenburger <wilde...@freakmail.dewrote:
vedrandeko...@v-programs.com wrote:
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
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

Jul 27 '07 #4
ve***********@v-programs.com wrote:
>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
Jul 27 '07 #5
On Jul 27, 11:26 am, Wildemar Wildenburger <wilde...@freakmail.de>
wrote:
vedrandeko...@v-programs.com wrote:
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
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.

Jul 28 '07 #6
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.

Jul 28 '07 #7
On 28 srp, 07:05, Zentrader <zentrad...@gmail.comwrote:
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
>>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"
>>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

Jul 28 '07 #8
ve***********@v-programs.com wrote:
On 28 srp, 07:05, Zentrader <zentrad...@gmail.comwrote:
>>>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
>>>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"
>>>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.
>>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'
>>>
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 -------------

Jul 28 '07 #9
>>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'
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/

Jul 28 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Scott | last post: by
2 posts views Thread by Raskolnikow | last post: by
51 posts views Thread by Alan | last post: by
27 posts views Thread by Paulo da Silva | last post: by
30 posts views Thread by galiorenye | last post: by
10 posts views Thread by Phillip Taylor | last post: by
17 posts views Thread by Chris M. Thomasson | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.