0

this is my code it prints with lots of space after OS Name: how to strip off the white space? was trying to use split, I guess did not use it in right way

OS Name:Microsoft Windows 7 Professional

See the Microsoft Knowledge Base article 2344941

#!/usr/bin/env python
import sys, re, os

lineContains = re.compile('.*OS Name.*')
lineHas = re.compile('.*OS Version.*')

filename = open("system-info.txt", 'r')
for line in filename:
    if lineContains.match(line):
        ''.join(line.split())
        print line
        if "Windows 7" in line:
            print 'See the Microsoft Knowledge Base article 2344941'
        else:
            print 'See the Microsoft Knowledge Base article 2550978'    
filename.close()

2 Answers2

0

You can use REGEX to strip of white space. myString.replaceAll("\\s+","") will eliminate multiple white spaces. However, there are white spaces that are not necessarily "spaces". For these you can use myString.replaceAll("\\W+", "").

vaultah
  • 44,105
  • 12
  • 114
  • 143
  • i tried replaceAll,**if lineContains.match(line): word = line.strip("\\W+", "") print word** gave me this error: **Traceback (most recent call last): File "hotfix.py", line 10, in word = line.strip("\\W+", "") TypeError: strip() takes at most 1 argument (2 given) ** – user3731311 Jun 24 '14 at 18:02
  • if lineContains.match(line): Name = re.sub('+',r': ',line) print Name if "Windows 7" in Name: print 'See the Microsoft Knowledge Base article 2344941' else: print 'See the Microsoft Knowledge Base article 2550978' **does not work this way** – user3731311 Jun 25 '14 at 20:30
0

My suggestion would be to use line = re.sub(r':[\t ]+',r': ',line). That will take any colon and all the whitespace after it and replace it with a single colon and a space so that you have a guaranteed amount of whitespace after every colon.

RevanProdigalKnight
  • 1,316
  • 1
  • 14
  • 23
  • i tried this with my code and it does not seem to work, really bad at regex so any help or instruction would be helpful thank you **'#!/usr/bin/env python import sys, re, os, string lineContains = re.compile('.*OS Name.*') lineHas = re.compile('.*OS Version.*') filename = open("system-info.txt", 'r') for line in filename: if lineContains.match(line): Name = re.sub(r':[\t]+',r': ',line) print Name if "Windows 7" in line: print 'See the Microsoft Knowledge Base article 2344941' else: print 'See the Microsoft Knowledge Base article 2550978' filename.close()'** @RevanProdigalKnight – user3731311 Jun 25 '14 at 16:10
  • @user3731311 The problem is that you made the results of the regex replace go into a new variable, `Name`, so while that variable had the correct result, the contents of `line` went unchanged because in Python all string manipulation returns a new string instead of modifying the original. I remember when I originally picked up Python that confused me as well. If you change the line `if "Windows 7" in line:` to `if "Windows 7" in Name:` or change `Name = re.sub(...)` to `line = re.sub(...)` it should work. – RevanProdigalKnight Jun 25 '14 at 18:34
  • still the same does not take out spaces after OS Name: Microsoft Windows 7.. – user3731311 Jun 25 '14 at 18:40
  • @user3731311 So that I can keep track, which change did you make? – RevanProdigalKnight Jun 25 '14 at 19:50
  • @user3731311 I also just now noticed that when you put in my regex line, you got rid of the space after `\t` in `r':[\t ]'`. That was actually supposed to be there and was not a typo; if you get rid of that it will ignore spaces. – RevanProdigalKnight Jun 25 '14 at 20:10
  • (+',r': ',line), this way? – user3731311 Jun 25 '14 at 20:16
  • @user3731311 I'm not entirely sure I know what you're trying to ask. Just copy everything in re.sub() from my answer and it should work. If not, I'll put up a pastebin link with code that should work and comments to explain what I'm doing. – RevanProdigalKnight Jun 25 '14 at 20:26
  • i tried it but does not work – user3731311 Jun 25 '14 at 20:33
  • @user3731311 if [this](http://pastebin.com/NkfYnDWB) doesn't work exactly as it is, then I don't know what you're looking for. – RevanProdigalKnight Jun 26 '14 at 12:13