a simple string question
Steve Holden
steve at holdenweb.com
Sat Jul 28 08:15:24 EDT 2007
vedrandekovic at v-programs.com wrote:
> On 28 srp, 07:05, Zentrader <zentrad... at gmail.com> wrote:
>>>> vedrandeko... at 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 -------------
More information about the Python-list
mailing list