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 53921
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.
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.
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?: -
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
Recognized Expert Moderator Specialist
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 cast like:
if ((sNo<>null) && (sNo<>""))
Have there any function to check the null and "" at once
|
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 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
|
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 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...
|
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: 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,...
|
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...
| |
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,...
|
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...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |