Python Forum
If this and that "case insensitive"
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
If this and that "case insensitive"
#1
Hi,
I’m trying to ignore all lines that have word CMV in .csv file, problem is some of the lines have small case “cmv”. Basically I’m splitting my file on coma and then if the string[1] has a “CMV” or “cmv” I need to ignore.
Here is the code, for some reason I cannot make the code to be case insensitive.
Thank you.
import os

f_ticket = 'C:/Tickets/'
f_clean  = open ('C:/Tc/T_Clean.csv','w')

mt_cap = 'CMV'
mt_sma = 'cmv'

for root,dirs,files in os.walk(f_ticket):
    for ft in files:
        tk_p = f_ticket+ft
        print ("CSV Files-- " ,tk_p)
        with open (tk_p,'r') as f_csv :  
            for ln_f_csv in f_csv :
                mysplit = ln_f_csv.split (",")
                if mt_cap and mt_sma in mysplit[1] :
                    print ("MATCHED -->> ",mysplit[1] )
                else :       
                    f_clean.write(ln_f_csv) 
f_clean.close()
Reply
#2
Try change line 16 to:
if mt_sma in mysplit[1].lower():
    print ("MATCHED -->> ",mysplit[1] )
Reply
#3
Thank you! It'll work!
Reply
#4
Do you understand why, though?
Reply
#5
I think Python cannot igone cases like Perl does with "i", that is why I had to convert the string to "lower() or upper()"
Right?
And thank you for the following up! that is why so many people like this forum.
Reply
#6
I haven't done much Perl, but I guess "i" is used with regular expressions. If you were using regular expressions here, you could also set a flag to do case-insensitive matches.

The reason that the code above works, though is that in strings upper and lower case characters are actually separate characters (in the Unicode charts). That means that two strings with the same characters differing in case won't compare equal.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Switch case or match case? Frankduc 9 4,541 Jan-20-2022, 01:56 PM
Last Post: Frankduc
  regex and case insensitive Leon79 8 3,237 Jul-20-2020, 11:04 AM
Last Post: Leon79

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020