I am seeing some solutions using re, out there on the Interwebs, but not what I want. I want to match several strings with partial content of a list item.

# as an example of one of the rows in a csv file
hostlist=[192.168.10.17, postgres12, "Red Hat 4.3", "broken"]
#pseudocode 
if hostlist[2] contains "Red Hat" or "Debian" or "Fedora" or "Linux"
    print(hostlist)
rowEdit=[hostlist[2], hostlist[0]]

The next few lines looked like a fruitful path, but it is not writing anything to csv:

                rappos=row[4]
                print(rappos)
                match = re.search('Linux', rappos)
                print(match)
                if match==True: 
                    print(rowEdit)  #rowEdit is a subset of the list 'row'
                    writer.writerow(rowEdit)

#output
Red Hat Enterprise Linux ES 3
(<_sre.SRE_Match object at 0x7f58b78c44a8>, ' is a match')
Red Hat Enterprise Linux ES 3
(<_sre.SRE_Match object at 0x7f58b78c49f0>, ' is a match')
Red Hat Enterprise Linux ES 3
(<_sre.SRE_Match object at 0x7f58b78c4a58>, ' is a match')
Red Hat Enterprise Linux ES 3
(<_sre.SRE_Match object at 0x7f58b78c4988>, ' is a match')
Red Hat Enterprise Linux ES 3
(<_sre.SRE_Match object at 0x7f58b78c49f0>, ' is a match')
Windows Server 2003 Service Pack 2
(None, ' is a match')
Windows Server 2003 Service Pack 2
(None, ' is a match')
Windows Server 2003 Service Pack 2
(None, ' is a match')
Windows Server 2003 Service Pack 2
(None, ' is a match')
Windows Server 2003 Service Pack 2
(None, ' is a match')
Windows Server 2003 Service Pack 2
(None, ' is a match')

So should I be saying

if match != "None":
    print(whatever)

The above if condition worked...
But I am not seeing the csv.writerow(rowEdit) function writing a csv file.

The https://github.com/wolf29/csv-edit/blob/master/reader3.py file is the real current code on this. I am getting the example code here a little messed up.

Recommended Answers

All 6 Replies

If you don't insist on using RE,

fid=open(<csv file>)
chklst=["Red Hat","Debian" , "Fedora" , "Linux"]
for row in fid:
     for o in chklst:
        if o in row:
           print row
           break
fid.close()

this still brings all rows, whether I sift through the Windows wchklist or the linux chklist.

def content(filename):
    with open('nix_content_'+filename, 'wb') as content:
        writer = csv.writer(content)
        with open(filename, 'rb') as mycsv:
            reader = csv.reader(mycsv)
            counter = 0
            for counter,row in enumerate(reader):
                if counter < 8: continue
#               if counter > ([-2:]) : break 
                rowEdit = [row[0],row[22],row[2], row[4], row[6], row[15], row[16], row[11], row[18], row[19], row[20], row[25], row[26], row[27], row[28], row[29], row[30], row[31]]
                chklist=["Red Hat Enterprise Linux ES 3", "Linux 2.4-2.6 / Embedded Device / F5 Networks Big-IP", "Linux 2.4-2.6 / SonicWALL", "Linux 2.6", "Red Hat Enterprise Linux ES 4", "Red Hat Enterprise Linux Server 5.8", "Linux*"]
                wchklist=["Windows 2003 Service Pack 2", "Windows 2008 R2 Enterprise Service Pack 1", "Windows Server 2003 Service Pack 2", "Windows Server 2008 R2 Enterprise 64 bit Edition Service Pack 1","Windows"]
                #rappos=row[4]
                #print(rappos)
                for i in chklist:
                    if i in row[4]:
                        print(rowEdit)
                        writer.writerow(rowEdit)

Your pseudo in running form (except last line which does not make sense for me):

# as an example of one of the rows in a csv file
hostlist=['192.168.10.17', 'postgres12', "Red Hat 4.3", "broken"]
if any(item in hostlist[2] for item in  ("Red Hat", "Debian", "Fedora", "Linux")):
    print(hostlist)

pyTony++
That worked. That was a huge and far simpler piece to the puzzel than I had been hunting. The part of the pseudo that didn't make sense to you is the wider context of parsing csv files.

def content(filename):
    with open('nix_content_'+filename, 'wb') as content:
        writer = csv.writer(content)
        with open(filename, 'rb') as mycsv:
            reader = csv.reader(mycsv)
            counter = 0
            for counter,row in enumerate(reader):
                if counter < 8: continue
#               if counter > ([-2:]) : break 
                rowEdit = [row[0],row[22],row[2], row[4], row[6], row[15], row[16], row[11], row[18], row[19], row[20], row[25], row[26], row[27], row[28], row[29], row[30], row[31]]
                chklist=["Red Hat Enterprise Linux ES 3", "Linux 2.4-2.6 / Embedded Device / F5 Networks Big-IP", "Linux 2.4-2.6 / SonicWALL", "Linux 2.6", "Red Hat Enterprise Linux ES 4", "Red Hat Enterprise Linux Server 5.8", "Linux*"]
                chklist2 = ("Red Hat Enterprise Linux ES 3", "Linux 2.4-2.6 / Embedded Device / F5 Networks Big-IP", "Linux 2.4-2.6 / SonicWALL", "Linux 2.6", "Red Hat Enterprise Linux ES 4", "Red Hat Enterprise Linux Server 5.8", "Linux*")
                wchklist=["Windows 2003 Service Pack 2", "Windows 2008 R2 Enterprise Service Pack 1", "Windows Server 2003 Service Pack 2", "Windows Server 2008 R2 Enterprise 64 bit Edition Service Pack 1","Windows"]
                if any(item in row[4] for item in chklist):
                #if i in row:
                    print(rowEdit)
                    writer.writerow(rowEdit)

If there is any piece missing it is the hard-coded chklists, that only will show the known Windows or Linuces in the environment. To solve the problem I would really like to be able to match any partial value of row[4] with the generic "Linux" or "Windows" so my script doesn't age unnaturally.

-Wolf

why do not use only 'Windows' or 'Linux' then instead of specific versions list?

# as an example of one of the rows in a csv file
hostlist=['192.168.10.17', 'postgres12', "Red Hat Enterprise Linux ES 3", "broken"]
if 'Linux' in hostlist[2]:
    print(hostlist)

I was getting a persistent error - it was ignoring me, but I will try it again tomorrow and see if I am just too tired to type it right.

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.