I'm attempint to write a python application that will open a .csv file and look at comun one for certain string. If the string match print the row to a new .csv file.

import csv

input_file = csv.DictReader(open("stats.csv"))

for row in input_file:
print row

Recommended Answers

All 14 Replies

#!/bin/python

import csv

inFile  = open('data.csv', "rb")
reader = csv.reader(inFile)
outFile  = open('data2.csv', "wb")
writer = csv.writer(outFile, delimiter='\t', quotechar='"', quoting=csv.QUOTE_ALL)

for row in reader:
    if "cat" in reader:
        writer.writerow(row)

inFile.close()
outFile.close()

Ok, so I am able to read a data file and write a data file. The problem I am having now is I'm looking for 3 different strings. If those strings exists anywhere in the row then it should print to the new file. Otherwise it should not.

This code writes every row to the output file if "cat" is found anywhere in the file.

for row in reader:
    if "cat" in reader:
        writer.writerow(row)

For three different strings, you would use something like

for row in reader:
    found = False
    for col in row:    
        if col in [string_1, string_2, string_3] and not found:
            writer.writerow(row)
            found = True

`

import csv

string_1 = ('OneTouch AT')
string_2 = ('LinkRunner AT')
string_3 = ('AirCheck')

inFile  = open('data.csv', "rb")
reader = csv.reader(inFile)
outFile  = open('data2.csv', "wb")
writer = csv.writer(outFile, delimiter='\t', quotechar='"', quoting=csv.QUOTE_ALL)

for row in reader:
    found = False
    for col in row:
        if col in [string_1, string_2, string_3] and not found:
            writer.writerow(row)
            found = True

inFile.close()
outFile.close()

Thank You, Ok here is where i am at. It seems to be working for the most part. What happens though if you don't necessarly know what the full string will be. For example lets say the string is "Wi_Fi Performance Testing OneTouch AT' but then later maybe its "Wired Performance Testing ONeTouch AT". So in other words we do not neccessarly know the full string name? Also is it possible to only write column 1 and column 2 of the row?

You can write

searched = ['onetouch at', 'linkrunner at', 'aircheck']

def does_match(string):
    stringl = string.lower()
    return any(s in stringl for s in searched)

for row in reader:
    if any(does_match(col) for col in row):
        writer.writerow(row[:2]) # write only 2 first columns
#!/usr/bin/python

import csv
import re

#string_1 = ('OneTouch AT')
#string_2 = ('LinkRunner AT')
#string_3 = ('AirCheck')

searched = ['OneTouch AT', 'LinkRunner AT', 'AirCheck']

def does_match(string):
    stringl = string.lower()
    return any(s in stringl for s in searched)

inFile  = open('data.csv', "rb")
reader = csv.reader(inFile)
outFile  = open('data2.csv', "wb")
writer = csv.writer(outFile, delimiter='\t', quotechar='"', quoting=csv.QUOTE_ALL)

#for row in reader:
#   found = False
#   for col in row:
#       if col in [string_1, string_2, string_3] and not found:
#           writer.writerow(row)
#           found = True

for row in reader:
    if any(does_match(col) for col in row):
        writer.writerow(row[:2]) # write only 2 first columns

inFile.close()
outFile.close()

Using the search code provided above the results are blank? Here let me provide a sample data set.

This is just a test set of data.
data.csv file <contents>

LinkRunner AT Video,10,20
Video OneTouch AT,1,2
OneTouch AT,200,300
LinkRunner AT,200,300
AirCheck,200,300
OPVXG,1,20
OptiFiber,2,300

So ideally the code would ignore the OPVXG and the OptiFiber. n Which is why I am searching for OneTouch AT, LinkRunner AT, and AirCheck.

When I run the avove script. There is no data in data2.csv

You must take the time to understand the code. I wrote lowercase strings in searched. If you don't understand the code, it is a waste of time.

Ok wait so it has to do with the fact I mixed case in the srings. Hmm. Ok let me try that. Yes you are correct I am a bit confused.

Ok, you are right as soon asI change back to lowercase it works. I'm sorry but I'm not folowing I'm a bit confused. hmmm.

Let me see if I am at least on the right track.

searched = ['onetouch at', 'linkrunner at', 'aircheck']

Is defines an array of strings called search correct? I'm not sure what the significance of case is yet

def does_match(string):
    stringl = string.lower()
    return any(s in stringl for s in searched)





    Here i'm lost. 

When the string is "Wired Performance Testing ONeTouch AT", the first line in does_match() calculates the same in lower case: "wired performance testing onetouch at". Then it checks if this string contains "onetouch at", or any of the other substrings. This way ONeTouch AT gives the same result as OneTouch AT for example.

Got it, that makes since now. One last piece here. I would like to print all the aircheck rows first then the linkrunner rows and finally the onetouch at rows. Out of curiousity how would you pic specific columns to print. Say I dunno column 1 and 5 or whatever? Someday I'll understand this :-)

To output the aircheck rows first, etc, one must store the rows as one examines them to determine which group they belong to. One can write

searched = ['aircheck', 'linkrunner at', 'onetouch at'] # ordered

def find_group(row):
    """Return the group index of a row
        0 if the row contains searched[0]
        1 if the row contains searched[1]
        etc
        -1 if not found
    """
    for col in row:
        col = col.lower()
        for j, s in enumerate(searched):
            if s in col:
                return j
    return -1

"""Built a list of items to sort. If row 12 contains 'LinkRunner AT' (group 1),
    one stores a triple (1, 12, row)
    When the triples are sorted later, all rows in group 0 will come first, then
    all rows in group 1, etc.
"""
stored = []
for i, row in enumerate(reader):
    g = find_group(row)
    if g >= 0:
        stored.append((g, i, row))
stored.sort()

for g, i, row in stored:
    writer.writerow(tuple(row[k] for k in (0,4))) # output col 1 & 5

Thank You so much. You have been a great deal of help. Do you happen to know of any good books on python or is the python documentation website about as good as it gets?

Dive into python is a well known free book online for beginners. The creator of python, Guido van Rossum recently reviewed and recommended another introductory book in his blog, if you want a real book.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.