Python - how to get rid of whitespace


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Python - how to get rid of whitespace
# 1  
Old 04-20-2009
Python - how to get rid of whitespace

Hello,

I am trying to figure out how to get rid of whitespace that is in certain areas (Title) of HTML pages that are created be a python script.

IE:

This is the title. (tabs are creating these spaces)

This is another title. (tabs are creating these spaces)
The python script is pulling from another database that inserts tabs.
I am looking for a line or two or code that will delete these tabs. I could manually go in and vi one of the pages but there are 1000's of pages.
# 2  
Old 04-20-2009
I gather you are not a python programmer - why not use sed -
Code:
sed 's/[    ]$//g' filename > newfile

The [ ] thing has two characters in it - a space and what you get from pressing the tab key.
# 3  
Old 04-20-2009
Quote:
Originally Posted by jhampt
IE:

This is the title. (tabs are creating these spaces)
so the tabs are at the end of the line? you can just use strip()
Code:
>>> astring="this is a title\t\t"
>>> repr(astring)
"'this is a title\\t\\t'"
>>> astring=astring.strip()
>>> repr(astring)
"'this is a title'"

# 4  
Old 04-21-2009
Quote:
Originally Posted by ghostdog74
so the tabs are at the end of the line? you can just use strip()
Code:
>>> astring="this is a title\t\t"
>>> repr(astring)
"'this is a title\\t\\t'"
>>> astring=astring.strip()
>>> repr(astring)
"'this is a title'"


No the tabs are in the middle of the Titles.

"This is [tab, tab] a title".

I am trying to turn that into:

"This is a title"

Of course there are 1000's of titles so they are not all the same but they all have the two tabs right in the middle of the titles.

Thanks for your assistance.
# 5  
Old 04-21-2009
Quote:
Originally Posted by jim mcnamara
I gather you are not a python programmer - why not use sed -
Code:
sed 's/[    ]$//g' filename > newfile

The [ ] thing has two characters in it - a space and what you get from pressing the tab key.

You would be correct. It was just handed off to me.

Since there are thousands of pages and titles this approach may not work?

thanks for your assistance.
# 6  
Old 04-21-2009
Quote:
Originally Posted by jhampt
No the tabs are in the middle of the Titles.

"This is [tab, tab] a title".

I am trying to turn that into:

"This is a title"

Of course there are 1000's of titles so they are not all the same but they all have the two tabs right in the middle of the titles.

Thanks for your assistance.
then just use split and join
Code:
>>> astring="this is\t\ta\t\ttime"
>>> print astring
this is         a               time
>>> print ' '.join(astring.split())
this is a time

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

Create a C source and compile inside Python 1.4.0 to 3.7.0 in Python for ALL? platforms...

Hi all... As you know I like making code backwards compatible for as many platforms as possible. This Python script was in fact dedicated for the AMIGA A1200 using Pythons 1.4.0, 1.5.2, 1.6.0, 2.0.1, and 2.4.6 as that is all we have for varying levels of upgrades from a HDD and 4MB FastRam... (1 Reply)
Discussion started by: wisecracker
1 Replies

2. Windows & DOS: Issues & Discussions

How to execute python script on remote with python way..?

Hi all, I am trying to run below python code for connecting remote windows machine from unix to run an python file exist on that remote windows machine.. Below is the code I am trying: #!/usr/bin/env python import wmi c = wmi.WMI("xxxxx", user="xxxx", password="xxxxxxx")... (1 Reply)
Discussion started by: onenessboy
1 Replies

3. Shell Programming and Scripting

**python** unable to read the background color in python

I am working on requirement on spreadsheet in python scripting. I have a spreadsheet containing cell values and with background color. I am able to read the value value but unable to get the background color of that particular cell. Actually my requirement is to read the cell value along... (1 Reply)
Discussion started by: giridhar276
1 Replies

4. UNIX for Dummies Questions & Answers

adding whitespace

Hi guys, I am working with large data sets and often times realize that not all of the columns are aligned correctly (sometimes rows will be shifted). So when I try to do something like: awk '{ if ($2 > 30 && $5 == $3){print}}' file > output it won't really work since some of the rows... (2 Replies)
Discussion started by: verse123
2 Replies

5. Shell Programming and Scripting

Getting rid of whitespace

Hello I am working aon script, that tells me how many users or on the system when i run it. The script is #!/bin/bash w | cut -f 1 -d ' ' |sort -u | wc -l When ran it shows 16 users including myself and a line of white space. I was wondering what I need to add to remove my user... (2 Replies)
Discussion started by: mosdojaf
2 Replies

6. Shell Programming and Scripting

How to match (whitespace digits whitespace) sequence?

Hi Following is an example line. echo "192.22.22.22 \"33dffwef\" 200 300 dsdsd" | sed "s:\(\ *\ \):\1:" I want it's output to be 200 However this is not the case. Can you tell me how to do it? I don't want to use AWK for this. Secondly, how can i fetch just 300? Should I use "\2"... (3 Replies)
Discussion started by: shahanali
3 Replies

7. Shell Programming and Scripting

Help with Python Code Whitespace

Hello All, I have some python code that pulls together titles and displays them on web pages. Here is the section of code I am struggling with: #string to grab title titlePattern =r'''\s*(\(+\))?(?P<title>.*)''' #returns the title part of the subject line def getTitle... (0 Replies)
Discussion started by: jhampt
0 Replies

8. Shell Programming and Scripting

Of bash and whitespace...

Hmmm... Bash doesn't parse whitespace with a read. lev@sys09:~$ read line; echo "$line" test test You can imagine what this does if you're using a shell script to read a list of unknown file names containing unknown spaces. lev@sys09:~$ read word1 word2; echo "$word1,$word2" 123 456... (2 Replies)
Discussion started by: lev_lafayette
2 Replies

9. Shell Programming and Scripting

trim whitespace?

I'm trying to find a command that will trim the white space off a string. e.g. $str = " stuf " $str = trim ( $str ) echo $str // ouput would just be stuf Thanks, Mark (4 Replies)
Discussion started by: msteudel
4 Replies
Login or Register to Ask a Question