Joe
Joe

Reputation: 17

How to compare column value in csv

I tend to compare two column value like:

 name        columnA     columnB
hahaha        [123]        123

one with square brackets,one without square brackets. Id like to verify the content in bracket(123) is same as the value under columnB(123)

First read these value:

with open('2.csv','rb') as csvfile:
reader3 = csv.DictReader(csvfile)
for row in reader3:
    if row['name']== 'hahaha':
        b=row['columnA']
        c=row['columnB']  

and I try to extract content from bracket by:

e = re.findall(r'\[([^]]*)\]',b)

I print c and e. output is ['123'] [123] now I dont know how to make ['123'] = [123]

Any ideas? Thanks!

Upvotes: 0

Views: 46

Answers (1)

cs95
cs95

Reputation: 402323

If you're looking to remove braces from a string, you could do so with str.strip -

>>> '[1234]'.strip('[]')
'1234'

As for your current method, your regular expression is the culprit here. I'm not sure what you think it does, but it definitely isn't right. One possible regex solution would be -

>>> re.match(r'\[(.*?)\]', '[1234]').group(1)
'1234'
\[      # opening brace 
(.*?)   # match-all capture group
\]      # closing brace 

Upvotes: 2

Related Questions