Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Ignore case in Python script

In my Python script, I’m working with JSON files but in some files I have an input "racecards" and in some files I have "raceCards".

How can I ask to Python to use the two cases?

My script:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

for file1 in FILE:
    with open(path + '%s' %file1,encoding='utf-8') as f1:
        INPUT1 = json.load(f1)

        DATA = INPUT1["pageProps"]["initialState"]["raceCards"]["races"]
        X = list(DATA.values())

        for x in X[0]:
            ENTRY = []
            for h1 in header1.split(','):
                ENTRY.append(x[h1])

            Entry = [str(i) for i in ENTRY]
            towrite = ','.join(Entry)
            print(towrite,file=output1)

>Solution :

One way is that you can simply use a try & except to implement it.

instead of:

DATA = INPUT1["pageProps"]["initialState"]["raceCards"]["races"]

Replace with:

try:
   DATA = INPUT1["pageProps"]["initialState"]["raceCards"]["races"]
except Exception ignored:
   DATA = INPUT1["pageProps"]["initialState"]["racecards"]["races"]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading