473,750 Members | 2,265 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I check for empty string?

1 New Member
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 53921
ilikepython
844 Recognized Expert Contributor
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.readli ne().strip()
FileData.close( )

testid = FileContents.sp lit(",")[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 Recognized Expert Expert
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 Recognized Expert Expert
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 Recognized Expert Contributor
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 Recognized Expert Moderator Specialist
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 New Member
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 New Member
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
34556
by: Hamed | last post by:
Hello How can I check if a string is empty? Thanks
2
3962
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 cast like: if ((sNo<>null) && (sNo<>"")) Have there any function to check the null and "" at once
5
10835
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
1969
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 can be Nothing but not in the sense of a object reference to Nothing ... rather, you supposedly can use an equality comparison MyControl.Date = Nothing
19
19345
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
16308
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
88612
by: noagbodjivictor | last post by:
How to check if a string is empty in python? if(s == "") ??
2
8108
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 modify my sql script because it is created dynamically. Below I have the old method to check for DBNull and to deal with DBNull in the recordset. This method of coding is used over 80 times as I have over 80 fields to check for DBNull, so a one...
15
13717
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
8838
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9396
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9339
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8260
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6804
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6081
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4887
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3322
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2804
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.