473,385 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How do I check for empty string?

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
7 53891
ilikepython
844 Expert 512MB
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
6,596 Expert 4TB
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
6,596 Expert 4TB
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
511 Expert 256MB
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
2,851 Expert Mod 2GB
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
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
126 100+
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

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Hamed | last post by:
Hello How can I check if a string is empty? Thanks
2
by: ad | last post by:
I have a string like string sNo. I want to cast it to int with int.Parse(sNo) but if the string is null or empty, it will thow exception. I must check if sNo is not null and is not empty before...
5
by: Shapper | last post by:
Hello, I am using this code line to check i a variable exists: If cookie Is Nothing Then .... How can I check if cookie is empty when cookie exists? Thanks, Miguel
6
by: John A Grandy | last post by:
how are people dealing with the situation where a function accepts a String representation of a date ... but a control on the page or form returns a Date value ... strangely, these Date values...
19
by: Dave | last post by:
If Iwant to check if dataset1.SelectQuery1.column1 == System.DBNull.Value. How do I do this? What I wrote above will give an error. -- L. A. Jones
2
by: shapper | last post by:
Hello, I need to check if a string is empty. Should I use: String.IsNullOrEmpty(MyString) or MyString Is Nothing? What is the difference?
31
by: noagbodjivictor | last post by:
How to check if a string is empty in python? if(s == "") ??
2
by: =?Utf-8?B?TWlrZSBPS0M=?= | last post by:
VB 2005 All developers face this issue; so I'm sure Microsoft was a solution for it. What is the built in method to check DBNull in a recordset field? NOT a typed dataset. I don't want to...
15
by: puzzlecracker | last post by:
What is the quickest way to check that the following: const line; only contains whitespace, in which case to ignore it. something along these lines: isspacedLine(line); Thanks
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.