0

I have want to write and store a simple if else output in a value. I have filenames like this.

US_Sales_2019090200022300023223155

UK_Sales_2019090200022300023223155

Sales_2019090210022300023223155

Sales_2019090210022300023223155

If Country is US or UK , I want to store the output value in Country as US or UK (Country1==file[:2]). If not I want to store the output value in Country as like 201909021 (Country2==file[5:13])

Please help me.

Country==[]
Country1==file[:2]
Country2==file[5:13]

if Country1=='US'|Country1=='UK':
     Country1==Country
else:
     Country2==Country
2
  • The assignment operator is =, not == (which is correct to compare inside the if, but not to assign values to variables). You also reversed the assignments inside the if statement, should be Country=Country1 and Country=Country2. Commented Sep 2, 2019 at 4:24
  • Hi, Could you please write the code. I am newbie. Thanks in advance Commented Sep 2, 2019 at 4:27

2 Answers 2

2

It is way easier to just scan the entire filename for the country-code instead of specifying where exactly the countrycode could be. Like this:

file = "UK_Sales_2019090200022300023223155"

if "US" in file:
    Country = "US"
if "UK" in file:
    Country = "UK"
# Add more country-codes, if needed    
else:
    Country = None

print(Country)

>>> UK
Sign up to request clarification or add additional context in comments.

Comments

0

A simple way is to split file name into pieces:

def get_val(fileX):
    if 'UK' in fileX or 'US' in fileX:
         return fileX.split('_')[0]
    else:
        vals = fileX.split('_')[1]
        return vals[:9] # next 9digits

# test
file1 = "UK_Sales_2019090200022300023223155"
file2 = "Sales_2019090210022300023223155"
print(get_val(file1))
print(get_val(file2))

Not depending upon your variable name, you can assign it to country or digit code/date may be.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.