School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,214 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,560 people online right now. Registration is fast and FREE... Join Now!




Simple string question

 

Simple string question

Scorpiion

17 Oct, 2009 - 10:00 AM
Post #1

D.I.C Head
**

Joined: 23 Apr, 2009
Posts: 62



Thanked: 1 times
My Contributions
Hi, I'm trying to make a simple battery warning program for my laptop, or script would be a better name. I'we done some python before but does not remember that much right now. My script looks like this:

CODE

import os

battery_os = os.popen("acpi -b")
for line in battery_os.readlines():
        battery = line

limit = "     Battery 0: Discharging, 5%"
print limit
print battery[1-31]

if limit == battery[1-31]:
        print 'Hello'


The idéa is simply to compare the output of "acpi -b" with gives something like "Battery 0: Charging, 7%, 02:08:13 until charged" and then compare this first part "Battery 0: Charging, 7%" for a special value before "%" and when those are the same do some kind of sound or why not eject the cd... tongue.gif Maybe I should add some kind of delay somewhere as well since it does not have to check the battery multiplie times per second.

So back to my question, how do I compare char 1-20 for example in a string with an other string?

CODE

if limit == battery[1-31]


ps. I know there are programs for this but right now I'm using fluxbox (quite simple Linux Window Manager) and I would like to do my own simple battery checker. ds.

User is offlineProfile CardPM
+Quote Post


chief27

RE: Simple String Question

17 Oct, 2009 - 10:57 AM
Post #2

New D.I.C Head
*

Joined: 13 Oct, 2009
Posts: 5



Thanked: 1 times
My Contributions
The easiest answer to this would be to use a regular expression. To find out how to use regular expressions in python read this link http://www.amk.ca/python/howto/regex/

It's definitely worth investing a little time in learning about regular expressions because it is a skill you can transfer to almost any programming language. The expressions themselves have more or less the same syntax across languages, although some languages do offer some additional features.
User is offlineProfile CardPM
+Quote Post

programble

RE: Simple String Question

17 Oct, 2009 - 04:25 PM
Post #3

D.I.C Regular
Group Icon

Joined: 21 Feb, 2009
Posts: 423



Thanked: 10 times
Dream Kudos: 50
My Contributions
I think you mean battery[1:31] not battery[1-31].
User is offlineProfile CardPM
+Quote Post

Scorpiion

RE: Simple String Question

18 Oct, 2009 - 06:20 AM
Post #4

D.I.C Head
**

Joined: 23 Apr, 2009
Posts: 62



Thanked: 1 times
My Contributions
QUOTE(programble @ 17 Oct, 2009 - 04:25 PM) *

I think you mean battery[1:31] not battery[1-31].


Thanks this was exactly what I was looking for! smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 10:02PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month