trimming help

This is the place for queries that don't fit in any of the other categories.

trimming help

Postby bbesase » Tue May 14, 2013 1:03 pm

If I have a list of strings such as:

Code: Select all
('', 'Cooper.References.UnitTests.vbproj')
('', 'Cooper.References.UnitTests.vbproj.user')
('', 'Cooper.References.UnitTests.vbproj.svn-base')
('', 'Cooper.References.UnitTests.vbproj.user.svn-base')


I have tried with rsplit and split but I cant seem to get it to work correctly.
I want to loop through it and wherever it contains letters after '.vbproj' I want to get rid of that string in the list. So the whole entire list should be made of files like so "blahblah.blah.UnitTests.vbproj"
Any help would be greatly appreciated, thanks in advance.
bbesase
 
Posts: 18
Joined: Wed May 08, 2013 1:57 pm

Re: trimming help

Postby stranac » Tue May 14, 2013 1:09 pm

You can use something like:
Code: Select all
if not filename.endswith('.UnitTests.vbproj'):
    # remove this item(or don't include it)

It's hard to be more specific without seeing the real data, as what you have shown doesn't look like a list of strings...
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 905
Joined: Thu Feb 07, 2013 3:42 pm

Re: trimming help

Postby bbesase » Tue May 14, 2013 1:48 pm

Code: Select all
from datetime import date
from optparse import OptionParser
import datetime
from datetime import datetime
import operator
import os

today = datetime.now().date()           #Gets todays date

path = "C:/Users/bgbesase/Documents/Brent/Code/Visual Studio/"
fileo = "C:/Users/bgbesase/Documents/Brent/UnitTest_Test/UnitTesting " + str(today) + ".doc"

def getUnitTest(path):
    foundFiles = []
    for r,d,f in os.walk(path):
        for files in f:
            if '.UnitTests.vbproj' in files:
                path2 = os.path.split(files)
                print path2
                foundFiles.append(path2)
    return foundFiles

def writeToFile(files):
    #getVB(files)
    getUnitTest(path)
    for path2 in files:
        fi= open(fileo, 'w')
        fi.truncate()
        fi.write('\n' + str(foundfiles))
        fi.close()

foundfiles=getUnitTest(path)

writeToFile(foundfiles)





here is the code, I'm sorry, I thought I could get away with not including everything ha.
bbesase
 
Posts: 18
Joined: Wed May 08, 2013 1:57 pm

Re: trimming help

Postby bbesase » Tue May 14, 2013 2:28 pm

I figured out why it's not working but I don't know to fix it. the list of strings which I want to loop through
Code: Select all
foundFiles []
isnt a single string so it wont work. I can't even call for the
Code: Select all
.endswith()
function since foundfiles isnt a string. Yet I don't know to do it :(
bbesase
 
Posts: 18
Joined: Wed May 08, 2013 1:57 pm

Re: trimming help

Postby stranac » Tue May 14, 2013 4:37 pm

Replace:
Code: Select all
if '.UnitTests.vbproj' in files:

with
Code: Select all
if files.endswith('.UnitTests.vbproj'):


Also, you'll need to fix your writeToFile() function.
The one you have right now makes little sense.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 905
Joined: Thu Feb 07, 2013 3:42 pm


Return to General Coding Help

Who is online

Users browsing this forum: cakefiend and 1 guest