Python if statement exclude "--" values

Dear all,

I'm reading data from a netcdf file, I've noticed some fields are written to my file as "--".

When I print the row to the console its value is:
[-33.939999, 151.03918, masked, masked, masked, masked, masked, masked, masked]

I've tried the below code block to check if the row doesn't contain masked or "--" and write those values.  Its not working!

How do I exclude or check for those values?

Thanks

if "masked" not in content:
   outputwriter.writerow(content)
   print content

Open in new window

AndyC1000Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

peprCommented:
The question is not clear. There is not enough information to answer. Please, show some example.
0
AndyC1000Author Commented:
Admin, I have provided a response at http:Q_28627100.html

To clarify this question the below code block writes a csv file.   My issue is when there is no data in the content string for a lat/lon, the row is outputted to the csv file as

[-33.939999, 151.03918, --, --, --, --, --, --, --]

or to the console as

[-33.939999, 151.03918, masked, masked, masked, masked, masked, masked, masked]

I tried wrapping the outputwriter around  an if statement i.e.

if "masked" not in content:
   outputwriter.writerow(content)
   print content

with open(r'C:/output.csv', 'wb') as csvFile:
    outputwriter = csv.writer(csvFile, delimiter=',')
    for date_val in date_strings:
        header.append(date_val)
    outputwriter.writerow(header)
    for lat_index, lat in enumerate(lats):
        for lon_index, lon in enumerate(lons):
            content = [lat,lon]
            for time_index, time in enumerate(times[:]): 
                data = value[time_index,lat_index,lon_index]
                content.append(data)
            outputwriter.writerow(content) 

Open in new window

0
aikimarkCommented:
please post a sample file
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

AndyC1000Author Commented:
from netCDF4 import Dataset, num2date
import csv

filename = "C:/Test.nc"

nc = Dataset(filename, 'r', Format='NETCDF4')

# get coordinates variables
lats = nc.variables['latitude'][:] 
lons = nc.variables['longitude'][:]

if nc.variables['latitude'].units != 'degrees_north':
    raise AttributeError('latitude units attribute not what was expected')

if nc.variables['longitude'].units != 'degrees_east':
    raise AttributeError('longitude units attribute not what was expected')

val = nc.variables['Min_SFC'][:]

times = nc.variables['time']
dates = num2date(times[:],times.units)
date_strings = [date.strftime('%d-%m-%Y') for date in dates]

header =  ['Latitude', 'Longitude']

with open(r'C:/output.csv', 'wb') as csvFile:
    outputwriter = csv.writer(csvFile, delimiter=',')
    for date_val in date_strings:
        header.append(date_val)
    outputwriter.writerow(header)
    for lat_index, lat in enumerate(lats):
        for lon_index, lon in enumerate(lons):
            content = [lat,lon]
            for time_index, time in enumerate(times[:]): 
                data = val[time_index,lat_index,lon_index]
                content.append(data)
            #outputwriter.writerow(content) 
            if "masked" not in content:
                outputwriter.writerow(content)
                print content

# close netcdf 
nc.close()

Open in new window

0
aikimarkCommented:
Please a sample DATA file.  Sorry for the confusion.
0
AndyC1000Author Commented:
Attached is the output data file, I'm not able to post the input data file (netcdf).
Output.csv
0
aikimarkCommented:
This will filter the csv file.  It is crude, but it does seem to work.  Of course, you would replace the print statement with a file-writing statement.
    import csv

    filename = "C:\Users\aikimark\Downloads\Output.csv"
    csvfile= open(filename, 'r')
    f=csv.reader(csvfile)
    for line in f:
        disposition = "drop"
        for item in line[2:]:
            if item <> "--":
                disposition = "keep"
                break
                
        print disposition, line

Open in new window

0
aikimarkCommented:
This is a little more elegant, using a comprehension:
    import csv

    filename = "C:\Users\aikimark\Downloads\Output.csv"
    csvfile= open(filename, 'r')
    f=csv.reader(csvfile)
    for line in f:
        disposition = "drop"
        if False in ["--" == xx for xx in line[2:]]:
            disposition = "keep"
                
        print disposition, line

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.