Solved

filter out unwanted rows

Posted on 2012-03-20
12
308 Views
Last Modified: 2012-03-21
Hello
Trying to remove unwanted rows from txt file.
eg

import csv

data = open("X:/r_dhwm.csv")

ma=[]
r = csv.DictReader(data,['Date','TEST','TEST2','TEST3'])
for a in r:
   
    if "{TEST}" == 'TEST':
        break
    else:
        ma.append("{TEST}".format(**a))


print(ma)
data.close()



RESULT =

['TEST', '', '', '', '', '', '', '', '-0.000412682', '-0.000393387']

What I want is

['-0.000412682', '-0.000393387']
0
Comment
Question by:philsivyer
  • 6
  • 6
12 Comments
 
LVL 28

Expert Comment

by:pepr
Comment Utility
Can you post a fragment of your csv file?  Can you comment on what data must be ignored?

The csv.DictReader() may be overkill for the purpose.  Anyway, the file must be opened for reading in binary mode.  Also the data is not a good identifier for the file object.

What version of Python do you use?  If it is new enough, you should prefer the with construct (the automatic file object close is done).

The following code
    if "{TEST}" == 'TEST':
        break
    else:
        ma.append("{TEST}".format(**a))

Open in new window


... is equivalent to

    ma.append("{TEST}".format(**a))

Open in new window


as the "{TEST}" == 'TEST' never holds.
0
 

Author Comment

by:philsivyer
Comment Utility
OK
I have attached a csv file - I want to be able to pick any column and return data to an array but with no headers (as in row 1 of csv file) or any cells that are null or blank (no values).
So, if I want to return data into array from column "TEST1" as in the attached my firsdt value world be: -0.011027536
Regards
0
 
LVL 28

Expert Comment

by:pepr
Comment Utility
I can see no attachment.
0
 

Author Comment

by:philsivyer
Comment Utility
Sorry
ee.csv
0
 
LVL 28

Expert Comment

by:pepr
Comment Utility
Try the following script:

a.py
import csv

def columnNonEmptyValues(csvFileName, columnName):
    with open(csvFileName, 'rb') as f:
         for d in csv.DictReader(f):
             value = d.get(columnName, '')  # default if not present
             if value:
                 yield value


if __name__ == '__main__':

    # Now you can iterate through the values of the chosen column.
    for value in columnNonEmptyValues('ee.csv', 'TEST1'):
        print value

    # Or you can pass the iterator to the list constructor.
    lst = list(columnNonEmptyValues('ee.csv', 'TEST1'))
    print '-' * 70
    print lst

Open in new window


It prints on my console:

c:\tmp\_Python\philsivyer\Q_27640149>python a.py
-0.011027536
-0.004086121
-0.012063901
-0.010020955
-0.002050665
-0.004273241
-0.009383166
-0.013938726
...
-0.055930797
-0.066374625
-0.070332921
-0.05798936
----------------------------------------------------------------------
['-0.011027536', '-0.004086121', '-0.012063901', '-0.010020955', '-0.002050665',
 '-0.004273241', '-0.009383166', '-0.013938726', '-0.03455181', '-0.045187455',
'-0.045987727', '-0.043084829', '-0.040607066', '-0.041589412', '-0.031441661',
'-0.032485216', '-0.015742733', '-0.01418973', '-0.012148488', '-0.010371504', '
-0.008392192', '-0.009299116', '-0.009473792', '-0.00851022', '-0.009960434', '-
0.009133718', '-0.005845811', '-0.005267109', '-0.0102875', '-0.014345424', '-0.
010280687', '-0.010643898', '-0.008002167', '-0.007472288', '-0.006297341', '-0.
017303705', '-0.021429973', '-0.013058342', '-0.002337027', '0.002957021', '-0.0
04385036', '-0.010007584', '-0.019457466', '-0.048419451', '-0.073353606', '-0.0
55930797', '-0.066374625', '-0.070332921', '-0.05798936']

Open in new window


If your Python does not support the with construct, try the older way:

...
def columnNonEmptyValues(csvFileName, columnName):
    f = open(csvFileName, 'rb')
    for d in csv.DictReader(f):
        value = d.get(columnName, '')  # default if not present
        if value:
            yield value
    f.close()

Open in new window

0
 

Author Comment

by:philsivyer
Comment Utility
Thanks - works a treat.
Question - excuse my ignorance but as a newbie to Python ...

can you explain this bit .. if __name__ == '__main__':

and ...
how does it know how to ignore null values or empty strings and not include the JARF header?

Regards
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:philsivyer
Comment Utility
Sorry - one more question
How can I write results to txt file

outfile = open("myresults.txt2,"wb")

etc ??

Regards
0
 
LVL 28

Accepted Solution

by:
pepr earned 500 total points
Comment Utility
> excuse my ignorance but as a newbie to Python ...
> can you explain this bit .. if __name__ == '__main__':


No need to appologise!  The purpose of EE is to find answers, to learn ;)  There is nothing like stupid or ignorant questions.

When you write a Python code and save it in the file like a.py, you can use the a.py or as a script (i.e. self-standing program), or as a module (via import a inside your other Python program).  The if __name__ ... is the way to get the best of both usages.  The __name__ is the attribut that takes the string value '__main__' if the file was used as a script, or the name of the module (source filename without .py) if it was used as a module.

When the Python file is processed, the parts of the code are compiled and executed.  The execution of the def... means creating an object that represents internally a function.  The lines like print... are exectuted "immediately".  This way, the if __name__ prevents the block of code to be executed when the a.py is used as a module.

If my previous example is stored as a.py, try the following b.py that uses a.py as a module:

b.py
import a

# Now you can iterate through the values of the chosen column.
f = open('output.txt', 'w')
for value in a.columnNonEmptyValues('ee.csv', 'TEST1'):
    f.write(value + '\n')
f.close()

# Or the more modern way...
with open('output2.txt', 'w') as f:
    for value in a.columnNonEmptyValues('ee.csv', 'TEST1'):
        f.write(value + '\n')


# Let's demonstrate the values of some arguments.
print '__name__ is', __name__
print 'a.__name__ is', a.__name__
print
print '__file__ is', __file__
print 'a.__file__ is', a.__file__
print
print "Value of __name__ == '__main__' is", __name__ == '__main__'

Open in new window

0
 
LVL 28

Expert Comment

by:pepr
Comment Utility
If the csv.DictReader(f) does not get the list of columns, it interprets the first line as the record with the column names.

If the d is a dictionary, the d['TEST1'] returns the value for the key 'TEST1'.  But it would fail if the key was not in the dictionary.  The d.get('TEST1', default) is the alternative that returns the same if the key exist or the given default if the item does not exist in the dictionary.

The object behaves as a boolean value in so called boolean context.  In other words, there is a boolean expression expected after the if command.  If it is not a boolean expression, the object interprets itself as a boolean value.  For strings, lists, and other sequence or containers, the empty value is interpreted as False, a non-empty value is interpreted as True.  This way, if the value was or read or set by default as empty string, it is interpreted as False, and it is not yielded by the generator (i.e. ignored).
0
 

Author Comment

by:philsivyer
Comment Utility
Thanks
Great response - helps a lot
0
 

Author Closing Comment

by:philsivyer
Comment Utility
Many thanks
0
 
LVL 28

Expert Comment

by:pepr
Comment Utility
You are welcome.  Have a good day. ;)
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

"The time has come," the Walrus said, "To talk of many things: Of sets--and lists--and dictionaries-- Of variable kinks-- And why you see it changing not-- And why so strange are strings." This part describes how variables and references (see …
Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now