Bottom Page

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
Quote
#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")
Quote
#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]
hbknjr likes this post
Quote
#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
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  macron on top of uppercase letters newbieAuggie2019 3 329 Aug-21-2019, 08:06 PM
Last Post: newbieAuggie2019
  'True' if 'string' has at least one uppercase letter ClassicalSoul 1 283 Feb-28-2019, 12:08 PM
Last Post: buran
  Is there a way to append to a list WITHOUT modifying the parameter passed? arnavb 11 1,070 Sep-23-2018, 07:16 AM
Last Post: Skaperen
  Help in understanding scope of dictionaries and lists passed to recursive functions barles 2 661 Aug-11-2018, 06:45 PM
Last Post: barles
  can't understand why 'str' is passed as parameter uddhavpgautam 2 791 Apr-16-2018, 05:15 PM
Last Post: nilamo
  Why args type is always tuple, when passed it as argument to the function. praveena 5 1,217 Jan-16-2018, 09:07 AM
Last Post: praveena
  What would be a way to check if a variable or class existed in an if? Klar 8 1,132 Dec-28-2017, 08:19 PM
Last Post: nilamo
  URL via raw_input, passed to HTTP requests and output? johnnyaustin 1 2,864 Dec-08-2017, 11:22 PM
Last Post: johnnyaustin
  [Variable as a String]>EscaSpecial characters printed CSA75 4 1,709 Mar-28-2017, 11:59 AM
Last Post: snippsat

Forum Jump:


Users browsing this thread: 1 Guest(s)