Python Forum
check if value of passed variable has uppercase characters in it.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
check if value of passed variable has uppercase characters in it.
#1
Greetings all,
I am parsing through lines in a file and what i am trying to do is look for any objects in position 5 (The line is getting split so it would be line[5]) and if line[5]contains an uppercase character anywhere in the name, I will then pass it to another portion of my script that will change the address to all lower case. So in the example below, line one would need to be changed
from: config firewall address trust edit "ADDRESS-216" set subnet 10.XX.181.163 255.255.255.255
to: config firewall address trust edit "address-216" set subnet 10.XX.181.163 255.255.255.255

below is a sample of the lines that are passed to the for loop:
config firewall address trust edit "ADDRESS-216" set subnet 10.XX.181.163 255.255.255.255
config firewall address trust edit "address-217" set subnet 10.XX.187.27 255.255.255.255
config firewall address trust edit "addRESS-218" set subnet 10.XX.69.163 255.255.255.255
config firewall address trust edit "ADDRess-219" set subnet 10.XX.75.27 255.255.255.255
config firewall address trust edit "ADDRESS" set subnet 10.XX.75.27 255.255.255.255
config firewall address trust edit "address" set subnet 10.XX.75.27 255.255.255.255

Below is a test where I have tried just using a list and doing a for loop to try to test changing the case with the following results. So I am not sure that isupper and isalpha work for my needs. But not sure how to get where I need to go.

listname = ['ONE', 'two', 'Thr33', 'four', 'FiVe368']

for test in listname:
    if test == test.isupper() and test.isalpha():
        print(test.lower())
    else:
        print(test + "is good")
        
and the output I am getting is:
ONE is good
two is good
Thr33 is good
four is good
FiVe368 is good

what I would want to see is
one
two is good
three
four is good
five368
Reply
#2
In Line 4: if test == test.isupper()
test = String
test.isupper() = Boolean

So, each time Python compares (string == Boolean) and False is returned. Therefore it jumps to else statement.

Here's the correct code.
listname = ['ONE', 'two', 'Thr33', 'four', 'FiVe368']
 
for test in listname:
    if test.isupper() and test.isalpha(): 
        print(test.lower())
    else:
        print(test + "is good")
Reply
#3
I think I figured it out since writing this:

import re
pattern = re.compile("([A-Z])")
listname = ['ONE', 'two', 'Thr33', 'four', 'FiVe368']

for test in listname:
    if pattern.match(test):
        print(test.lower())
    else:
        print(test + " is good")
The output is:
one
two is good
thr33
four is good
five368
[Finished in 0.2s]
Reply
#4
you shouldnt need to use regex for simple string checks.

Because str.lower() does not effect string'd digits, you can just check for upper
listname = ['ONE', 'two', 'Thr33', 'four', 'FiVe368']
  
for name in listname:
    if any(char.isupper() for char in name):
        print(name.lower())
    else:
        print("{} is good".format(name))
Output:
one two is good thr33 four is good five368
If you really had to check digit you could do that as well
listname = ['ONE', 'two', 'Thr33', 'four', 'FiVe368', '67', '245245']
  
for name in listname:
    if any(char.isdigit() or char.isupper() for char in name):
        print(name.lower())
    else:
        print("{} is good".format(name))
Output:
one two is good thr33 four is good five368 67 245245
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to receive two passed cmdline parameters and access them inside a Python script? pstein 2 278 Feb-17-2024, 12:29 PM
Last Post: deanhystad
  How do I check if the first X characters of a string are numbers? FirstBornAlbratross 6 1,427 Apr-12-2023, 10:39 AM
Last Post: jefsummers
  detecting a generstor passed to a funtion Skaperen 9 3,465 Sep-23-2021, 01:29 AM
Last Post: Skaperen
  AttributeError: module 'string' has no attribute 'uppercase' Anldra12 10 10,111 Apr-23-2021, 05:30 PM
Last Post: ibreeden
  how to modify a variable that is passed as parameter StefanL38 2 2,064 Dec-07-2020, 08:39 AM
Last Post: StefanL38
  check pandas variable type cools0607 3 6,510 Jun-12-2020, 09:28 AM
Last Post: buran
  Remove escape characters / Unicode characters from string DreamingInsanity 5 13,416 May-15-2020, 01:37 PM
Last Post: snippsat
  Python uppercase conversion conditions Jaypeng 7 2,891 Apr-29-2020, 11:24 AM
Last Post: jefsummers
  Check for a special characters in a column and flag it ayomayam 0 2,015 Feb-12-2020, 03:04 PM
Last Post: ayomayam
  Check for funny characters with a regexp bertilow 4 2,711 Jan-19-2020, 10:16 AM
Last Post: bertilow

Forum Jump:

User Panel Messages

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