-1
import os

pwd1 = str()
pwd2 = None
pwd3 = ""
pwd4 = " "

print("1:%s" %pwd1)
print("2:%s" %pwd3)
print("3:%s" %pwd3)
print("4:%s" %pwd4)

Test.txt file contents:

[Section1]
username : testuser
password : validpwd

[Section2]
username : testuser
password : validpwd

[Section3]
username : testuser
password : 

[Section4]
username : testuser
password : validpwd

Section3 contains password : as empty line.

I have parsed the value of the "password" field from Section3, and when I tried to print the "password" value, it prints as empty line in output. I need it to mention it as "None". How can I do that?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • 2
    Does this answer your question? [How to check if the string is empty?](https://stackoverflow.com/questions/9573244/how-to-check-if-the-string-is-empty) – mkrieger1 May 04 '20 at 08:35

1 Answers1

0

What you can do is you can check the length of password, while parsing. And if the length is <1, just assign it the value None.

psd = ''
psd = psd.strip()
if len(psd)<1:
    psd= 'None' # you can also just use None without any quotes
print(psd)
Pankaj Mishra
  • 445
  • 6
  • 15