473,395 Members | 1,484 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 1161
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Scott | last post by:
The following is the XML I have to work with. Below is the question <Table0> <CaseID>102114</CaseID> <CaseNumber>1</CaseNumber> <DateOpened>2005-06-14T07:26:00.0000000-05:00</DateOpened>...
2
by: Raskolnikow | last post by:
Hi! I have a very simple problem with itoa() or the localtime(...). Sorry, if it is too simple, I don't have a proper example. Please have a look at the comments. struct tm *systime; time_t...
51
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct...
1
by: Brian Henry | last post by:
Hello, I was tring to learn socket's (being i never used them before) and have a simple question. I want to create a listner that will get any data recieved and print it out. I've been able to...
4
by: dba_222 | last post by:
Dear Experts, Ok, I hate to ask such a seemingly dumb question, but I've already spent far too much time on this. More that I would care to admit. In Sql server, how do I simply change a...
27
by: Paulo da Silva | last post by:
Hi! I was told in this NG that string is obsolet. I should use str methods. So, how do I join a list of strings delimited by a given char, let's say ','? Old way:
4
by: SM | last post by:
Hello, I have a simple question, but can't find the answer. I have a string that contains a path to a file I want to add another string to the end of that string So, if i have : path =...
30
by: galiorenye | last post by:
Hi, Given this code: A** ppA = new A*; A *pA = NULL; for(int i = 0; i < 10; ++i) { pA = ppA; //do something with pA
10
by: Phillip Taylor | last post by:
Hi guys, I'm looking to develop a simple web service in VB.NET but I'm having some trivial issues. In Visual Studio I create a web services project and change the asmx.vb file to this: Imports...
17
by: Chris M. Thomasson | last post by:
I use the following technique in all of my C++ projects; here is the example code with error checking omitted for brevity: _________________________________________________________________ /*...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.