Surely a very easy string manip question

This is the place for queries that don't fit in any of the other categories.

Surely a very easy string manip question

Postby mimilala » Tue Oct 15, 2013 9:43 am

Hi,
I'm not a python newbie, actually I don't know anything about this language, but I have to correct my colleague's python script while he is on holidays /o\
The script creates database statistics and puts the result in an excel file with several sheets. I understood that the error happens in the following line:
Code: Select all
    for center in CENTERS:
>        sheet = wb.add_sheet(center)

Because the "center" variable can contain slashes which are not allowed as sheet names. So I would like to replace the line by something like:
Code: Select all
sheet = wb.add_sheet(center.replace-slash-by-space)

How can I do that? If I can avoid having to go through Python tutorials that would be great :)
Thanks,
Mimi
Last edited by Mekire on Tue Oct 15, 2013 10:02 am, edited 1 time in total.
Reason: First post lock
mimilala
 
Posts: 3
Joined: Tue Oct 15, 2013 9:06 am

Re: Surely a very easy string manip question

Postby mimilala » Tue Oct 15, 2013 10:32 am

Actually I found a solution in the meantime. I succeeded to use a regular expression, which allows me to shorten the string at the same time:
Code: Select all
        label = re.sub('xxx.*','xxx',center)
        sheet = wb.add_sheet(label)

That did the trick.
mimilala
 
Posts: 3
Joined: Tue Oct 15, 2013 9:06 am

Re: Surely a very easy string manip question

Postby Kebap » Tue Oct 15, 2013 11:39 am

Nice job. Here is another idea to your original question. For such easy tasks, I don't bother with regular expressions.

Code: Select all
>>> x = "Text with some / slashes // here"
>>> print x
Text with some / slashes // here
>>> print x.replace("/", "-")
Text with some - slashes -- here
>>>
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 375
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: Surely a very easy string manip question

Postby mimilala » Tue Oct 15, 2013 1:26 pm

Thanks Kebap, that's exactly what I had been looking for :)
I will stick to the regex though because it allowed me to shorten the string at the same time because it not only contained slashes but was actually also way too long for a sheet name.
Cheers,
Mimi
mimilala
 
Posts: 3
Joined: Tue Oct 15, 2013 9:06 am


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot], metulburr and 3 guests