Python Tips : Regular Expression usage to exclude something

Let’s say we want to match every single rain variation ..

“Isolated rain over coastal area” is rain.
“Scattered rain over inland area” is rain.
“Rain” is rain
However..
but not “No rain” .. is Fair weather ..

#!/usr/bin/python
import re
z = 1
y = 2
x = 3
 
if (z == 1) & (x == 3) & (y == 2):
 print "match"
else:
 print "not match"
 
norain = 'No rain'
rain = 'Rain'
rainfall = 'isolated rain interne'
 
zrain  =re.compile ('(?<!No\s)[Rr]ain')
m_rain = zrain.search (rainfall)
m2_rain = zrain.search (rain)
m1_rain = zrain.search (norain)
if m_rain :
  print "got rainfall"
if m2_rain :
  print "got me : Rain"
if m1_rain:
  print "no rain also defined as rain"
else :
        print "No rain is :not raining OK!"

result would be something like follows :

Get Free Email Updates!

Signup now and receive an email once I publish new content.

I will never give away, trade or sell your email address. You can unsubscribe at any time.

Like

Related Post

2 Responses

  1. namran says:

    the above function can be simplified a but further with the following :

    #!/usr/bin/python
     
    def getBM (eng): 
        if eng == 'No rain':
            return "Tiada Hujan"
        if eng == 'Rain':
            return "Hujan"
        if eng == 'fair':
            return "Cerah" 
        if eng == 'Weather Fair':
            return "Cuaca Baik"
        if eng == 'Clear':
            return "Cerah"
        if eng == 'Hazy':
            return "Berjerebu"
        if eng == 'Windy':
            return "Berangin"
        if eng == 'No rain':
            return "Tiada Hujan"
        if eng == 'Cloudy':
            return "Mendung"
        if eng == 'Cloudy over coastal areas':
            return "Mendung di kawasan pantai"
        if eng == 'Cloudy over inland areas':
            return "Mendung di kawasan pedalaman"
        if eng == 'Rain':
            return "Hujan"
        if eng == 'Intermittent rain occasionally moderate':
            return "Hujan sekejap-sekejap kadangkala sederhana"
        if eng == 'Intermittent rain occasionally heavy':
            return "Hujan sekejap-sekejap kadangkala lebat"
        if eng == 'Windspread intermittent rain':
            return "Hujan sekejap-sekejap yang menyeluruh"
        if eng == 'Isolated rain':
            return "Hujan di satu dua tempat"
        if eng == 'Isolated rain over coastal areas':
            return "Hujan di beberapa tempat di kawasan pantai"
        if eng == 'Isolated rain over inland areas':
            return "Hujan di beberapa tempat di kawasan pedalaman"
        if eng == 'Scattered rain':
            return "Hujan curah/sementara di satu dua tempat"
        if eng == 'Scattered rain over coastal areas':
            return "Hujan curah/sementara di kawasan pantai"
        if eng == 'Scattered rain over inland areas':
            return "Hujan curah/sementara di kawasan pedalaman"
        if eng == 'Showers':
            return "Hujan curah"
        if eng == 'Isolated showers':
            return "Hujan curah di satu dua tempat"
        if eng == 'Isolated showers over coastal areas':
            return "Hujan curah di kawasan pantai"
        if eng == 'Isolated showers over inland areas':
            return "Hujan curah di kawasan pedalaman"
        if eng == 'Thunderstorms':
            return "Ribut petir"
        if eng == 'Isolated thunderstorms':
            return "Ribut petir di satu dua tempat"
        if eng == 'Scattered thunderstorms':
            return "Ribut petir di beberapa tempat"
        if eng == 'Widespread thunderstorms':
            return "Ribut petir di kebanyakan tempat"
        else:
            return eng
     
    # example usage : 
    print "translating 'No rain' = " + getBM("No rain")
    print "translating 'No rain' = " + getBM("Scattered thunderstorms")
    print "translating 'No rain' = " + getBM("Scattered rain over inland areas")

    hmm. if putting into a file and read from there would be much more better.

    Reply
  2. namran says:

    yeah..

    to make it read from a text file and behave exactly as above.

    #!/usr/bin/python
     
    import csv
    # to read from a file with list of weather.
    reader = csv.reader(open("weather_list.txt", "rb"), delimiter=':', quoting=csv.QUOTE_NONE)
    # build up a reference list
    kamus = {}
    for row in reader:
     english_w=row[0].lstrip(' ').strip(' ')
     bm_w=row[1].lstrip(' ').strip(' ')
     kamus[english_w] = bm_w
     
    # a function to return exactly the same
    def getBM (eng) :
     return kamus[eng]
    # example usage : 
    print getBM ("Isolated showers");
    print getBM ("Rain");

    then create text file with the following content.

    [namran@nb-namran Desktop]$ cat weather_list.txt 
    Weather Fair:Tiada Hujan
    Clear:Cerah
    Hazy:Berjerebu
    Windy:Berangin
    No rain:Tiada Hujan
    Cloudy:Mendung
    Cloudy over coastal areas:Mendung di kawasan pantai
    Cloudy over inland areas:Mendung di kawasan pedalaman
    Rain:Hujan
    Intermittent rain occasionally moderate:Hujan sekejap-sekejap kadangkala sederhana
    Intermittent rain occasionally heavy:Hujan sekejap-sekejap kadangkala lebat
    Widespread intermittent rain:Hujan sekejap-sekejap yang menyeluruh
    Isolated rain:Hujan di satu dua tempat
    Isolated rain over coastal areas:Hujan di beberapa tempat di kawasan pantai
    Isolated rain over inland areas:Hujan di beberapa tempat di kawasan pedalaman
    Scattered rain:Hujan curah/sementara di satu dua tempat
    Scattered rain over coastal areas:Hujan curah/sementara di kawasan pantai
    Scattered rain over inland areas:Hujan curah/sementara di kawasan pedalaman
    Showers:Hujan curah
    Isolated showers:Hujan curah di satu dua tempat
    Isolated showers over coastal areas:Hujan curah di kawasan pantai
    Isolated showers over inland areas:Hujan curah di kawasan pedalaman
    Thunderstorms:Ribut petir
    Isolated thunderstorms:Ribut petir di satu dua tempat
    Scattered thunderstorms:Ribut petir di beberapa tempat
    Widespread thunderstorms:Ribut petir di kebanyakan tempat
    Reply

Anything to add?

X