364,111 Members | 2059 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

How do I check for empty string?

lphang
P: 1
I am reading a simple text file that will contains three set of string values
as follow:
"1234567", "ABC123456", ""

I have my codes as follow trying to check for an empty string, with the input string as above, I can never get it to display "testid is empty", Any idea why the test failed?:
Expand|Select|Wrap|Line Numbers
  1. FileData = open(filename, "r")
  2. Filecontents = FileData.readline().strip()
  3. FileData.close()
  4.  
  5. testid = FileContents.split(",")[2]
  6. if (testid == ""):
  7.    print "testid is empty"
  8. else:
  9.    print "testid has value"
Mar 14 '07 #1
Share this Question
Share on Google+
7 Replies


ilikepython
Expert 100+
P: 732
I am reading a simple text file that will contains three set of string values
as follow:
"1234567", "ABC123456", ""

I have my codes as follow trying to check for an empty string, with the input string as above, I can never get it to display "testid is empty", Any idea why the test failed?:

FileData = open(filename, "r")
Filecontents = FileData.readline().strip()
FileData.close()

testid = FileContents.split(",")[2]
if (testid == ""):
print "testid is empty"
else:
print "testid has value"
Im not exactly sure but first u should put it in code brackes when u post.
Why are you stripping it twice? Maybe im wrong but i think u should strip it once.
Other than that i dont know, maybe someone with more experience can answer this.
Mar 14 '07 #2

bartonc
Expert 5K+
P: 5,734
Im not exactly sure but first u should put it in code brackes when u post.
You are right. You can use the # button in Enhanced Mode. Thanks for the input.
Mar 14 '07 #3

bartonc
Expert 5K+
P: 5,734
I am reading a simple text file that will contains three set of string values
as follow:
"1234567", "ABC123456", ""

I have my codes as follow trying to check for an empty string, with the input string as above, I can never get it to display "testid is empty", Any idea why the test failed?:
Expand|Select|Wrap|Line Numbers
  1. FileData = open(filename, "r")
  2. Filecontents = FileData.readline().strip()
  3. FileData.close()
  4.  
  5. testid = FileContents.split(",")[2]
  6. if (testid == ""):
  7.    print "testid is empty"
  8. else:
  9.    print "testid has value"
That is a very good question since:

>>> testStr = "hello, world,"
>>> testList = testStr.split(",")
>>> testList
['hello', ' world', '']
>>> testList[2] == ""
True
>>>

The best way to track these things down is to make lots of asignments as I have done here and print them as you go.
Look for an empty
Mar 14 '07 #4

ghostdog74
Expert 100+
P: 511
I am reading a simple text file that will contains three set of string values
as follow:
"1234567", "ABC123456", ""

I have my codes as follow trying to check for an empty string, with the input string as above, I can never get it to display "testid is empty", Any idea why the test failed?:
Expand|Select|Wrap|Line Numbers
  1. FileData = open(filename, "r")
  2. Filecontents = FileData.readline().strip()
  3. FileData.close()
  4.  
  5. testid = FileContents.split(",")[2]
  6. if (testid == ""):
  7.    print "testid is empty"
  8. else:
  9.    print "testid has value"

its best to show a sample of what the file contents look like.
Is it like this?:
Expand|Select|Wrap|Line Numbers
  1. "1234567", "ABC123456", ""
  2.  
if it is , the when you read the file and split them up, the double quotes "" will be of string type and its length will be 2. An empty string should be of length 0. you could also check for len(testid) == 0, for empty string.
Also, you can put some print statements so you could debug your code
Expand|Select|Wrap|Line Numbers
  1. ....
  2. testid = FileContents.split(",")[2]
  3. print "testid value " , testid , type(testid), "length" , len(testid)
  4. ....
  5.  
Mar 15 '07 #5

bvdet
Expert Mod 2.5K+
P: 2,509
I did this in interactive as an example of reading the literal string below from a file:
Expand|Select|Wrap|Line Numbers
  1. "1234567", "ABC123456", ""
Expand|Select|Wrap|Line Numbers
  1. >>> s = raw_input()
  2. >>> s
  3. '"1234567", "ABC123456", ""'
  4. >>> sList = s.replace('"', '').split(', ')
  5. >>> sList
  6. ['1234567', 'ABC123456', '']
  7. >>> sList[2]
  8. ''
  9. >>> len(sList[2])
  10.  
  11. >>> 
Good advice ghostdog.
Mar 15 '07 #6

bladez
P: 1
the easiest way to check if its empty is simple :

Expand|Select|Wrap|Line Numbers
  1. _string = ""
  2.  
  3. if len(_string) <= 0:
  4.   print "Empty String"
  5. else:
  6.   print "Not Empty String"
  7.  
  8. Output:
  9.   Empty String
  10.  
Feb 8 '12 #7

Smygis
100+
P: 121
The even MOAR simplelestest way of it is to simply do

Expand|Select|Wrap|Line Numbers
  1. string = ""
  2.  
  3. if string:
  4.     do stuff
  5.  
stuff will not be done because an empty string is False

Expand|Select|Wrap|Line Numbers
  1. >>> s = ""
  2. >>> if s:
  3.     print "a"
  4.  
  5.  
  6. >>>
  7. >>> bool("")
  8. False
  9. >>> bool("a")
  10. True
  11. >>> 
btw, whoa, necroposting, like a boss.
Feb 8 '12 #8

Post your reply

Help answer this question



Didn't find the answer to your Python question?

You can also browse similar questions: Python