4 Replies - 702 Views - Last Post: 27 May 2010 - 02:50 PM Rate Topic: -----

#1 thepeon  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 04-February 08

Matching a line in a file

Posted 26 May 2010 - 06:23 PM

I'm trying to read a file and compare each line to a string. If it matches, I want to change a vareiable and exit, but if not, it should do something else. Long story short, I'm trying to prevent something from being sent if it has been sent in the past. Instead, I'm jumping straight to the "#do something" section of my code.

sl is defined already as a file object, and t is a string.

sent = False
for line in sl:
    if line == t:
        sent = True
if sent == False:
    #do something
    sl.write(t)



If there is a better way to do this, and I'm completely off course, please let me know. I have been teaching myself python and can't get this section of my code to work like it should.

Is This A Good Question/Topic? 0
  • +

Replies To: Matching a line in a file

#2 atik97  Icon User is offline

  • ???
  • member icon

Reputation: 144
  • View blog
  • Posts: 715
  • Joined: 16-September 08

Re: Matching a line in a file

Posted 26 May 2010 - 07:40 PM

Look for the code below, it will process the code when the condition of if statement fails. If a match is found, the rest of the code will be ignored and proceed to next iteration.

for line in sl:
    if line==t:
        continue
    #do something
    sl.write(t)


Was This Post Helpful? 0
  • +
  • -

#3 thepeon  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 04-February 08

Re: Matching a line in a file

Posted 27 May 2010 - 04:04 AM

For some reason it's still skipping to the "#do something". My file ends up with a lot of identical lines, which isn't supposed to happen, because if there is a match, the new version shouldn't be written to the file. It like the line "if line==t" always fails, and I don't know why.
Was This Post Helpful? 0
  • +
  • -

#4 atik97  Icon User is offline

  • ???
  • member icon

Reputation: 144
  • View blog
  • Posts: 715
  • Joined: 16-September 08

Re: Matching a line in a file

Posted 27 May 2010 - 05:22 AM

As the condition of if statement is checking against the variable 't' which holds a string, the condition will only match when there is exact match, which means the two values must be exactly same. Make sure you are getting the expected value in the variable t by printing its value. Also it would be easier to track the errors if you share details code.
Was This Post Helpful? 0
  • +
  • -

#5 thepeon  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 04-February 08

Re: Matching a line in a file

Posted 27 May 2010 - 02:50 PM

The variable is coming from the same source every time, so it should be the same, unless it is being altered when it is saved to the file.

#!/usr/bin/python
# tweet.py
# Author: thepeon (Michael Wilson, mwilsonemt@gmail.com)
# usage: Post Severe weather statments from NWS to Twitter, shorting the Watch/Warning URL using is.gd
# inpired by: A need to distribute severe weather information to Generations X and Y
 
import urllib, urllib2, sys, xml.dom.minidom, array, datetime, re
from os import popen

#twitter user credentials
user = "twitterHandle"
password = "password"

#geographic location that message is ment for
location = "DUPAGE, IL"

#Files, error and sent respectivly
el = open('/storage/small/wxScripts/wxErrorLog.txt', 'a')
sl = open('/storage/small/wxScripts/wxSentMessageLog.txt', 'a+')

datetime = datetime.datetime.now()

def tweet(message, user, password):
    url = 'http://twitter.com/statuses/update.xml' 
    curl = 'curl -s -u %s:%s -d status="%s" %s' % (user,password,message,url)
 
    pipe = popen(curl, 'r')

    if len(message) > 140:
        open(el, 'a')
        el.seek(0)
        el.write(datetime.month, '/', datetime.day, '/', datetime.year, '  ', datetime.hour, ':', datetime.minute, "     Message too long\n")
        el.colse()
        sys.exit()

def short(wwurl):
    apiurl = "http://is.gd/api.php?" + urllib.urlencode(dict(longurl=wwurl))  
    shorturl = urllib2.urlopen(apiurl).read()
    return shorturl
 
def getRss():
    address = 'http://www.weather.gov/alerts-beta/wwaatmget.php?x=ILC043'
    document = xml.dom.minidom.parse(urllib.urlopen(address))
    x = 0
    for entry in document.getElementsByTagName('entry'):
	title = entry.getElementsByTagName('title')[0].firstChild.data
	link = entry.getElementsByTagName('id')[0].firstChild.data
	t=title.split('http:')
	message = 'FOR '+ location +': ' + t[0] + short(link)
	
	for line in sl:
	    if line==t[0]:
		continue
	    tweet(message, user, password)
	    sl.write(t[0]) 
	    continue
getRss()
el.close()
sl.close()


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1