|
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?: -
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"
| |
Share this Question
| 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.
| | | 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.
| | | 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?: -
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
| | | 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?: -
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)
-
....
-
| | | Expert Mod 2.5K+
P: 2,509
|
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.
| | |
P: 1
|
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
-
| | | 100+
P: 121
|
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.
| | Post your reply Help answer this question
Didn't find the answer to your Python question?
You can also browse similar questions: Python | | Question stats - viewed: 32213
- replies: 7
- date asked: Mar 14 '07
|