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"
7 53891
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.
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.
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"
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
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"
its best to show a sample of what the file contents look like.
Is it like this?: -
"1234567", "ABC123456", ""
-
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 -
....
-
testid = FileContents.split(",")[2]
-
print "testid value " , testid , type(testid), "length" , len(testid)
-
....
-
bvdet 2,851
Expert Mod 2GB
I did this in interactive as an example of reading the literal string below from a file: - "1234567", "ABC123456", ""
- >>> s = raw_input()
-
>>> s
-
'"1234567", "ABC123456", ""'
-
>>> sList = s.replace('"', '').split(', ')
-
>>> sList
-
['1234567', 'ABC123456', '']
-
>>> sList[2]
-
''
-
>>> len(sList[2])
-
-
>>>
Good advice ghostdog.
the easiest way to check if its empty is simple : -
_string = ""
-
-
if len(_string) <= 0:
-
print "Empty String"
-
else:
-
print "Not Empty String"
-
-
Output:
-
Empty String
-
The even MOAR simplelestest way of it is to simply do -
string = ""
-
-
if string:
-
do stuff
-
stuff will not be done because an empty string is False - >>> s = ""
-
>>> if s:
-
print "a"
-
-
-
>>>
-
>>> bool("")
-
False
-
>>> bool("a")
-
True
-
>>>
btw, whoa, necroposting, like a boss.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Hamed |
last post by:
Hello
How can I check if a string is empty?
Thanks
|
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...
|
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
|
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...
|
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
|
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?
|
by: noagbodjivictor |
last post by:
How to check if a string is empty in python?
if(s == "") ??
|
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...
|
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
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
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...
|
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,...
|
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...
|
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,...
|
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...
| |