473,378 Members | 1,426 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,378 software developers and data experts.

catching empty strings (I guess that's what they are)

I've began accepting user input :( in an old program. The input comes
from a simple text file where users enter filenames (one per line). What
is the appropriate way to handle blank lines that hold whitespace, but
not characters? Currently, I'm doing this:

for user_file in user_files:
# Remove whitespace and make lowercase.
file_skip_list.append(user_file.strip().lower())

file_skip_list = list(sets.Set(file_skip_list))

However, if the input file has blank lines in it, I get this in my list:

'' (that's two single quotes with noting in between)

I thought I could do something like this:

if user_file == None:
pass

Or this:

if user_file == '':
pass

But, these don't work, the '' is still there. Any suggestions are
appreciated!

Brad
Jul 9 '07 #1
3 933
brad wrote:
I've began accepting user input :( in an old program. The input comes
from a simple text file where users enter filenames (one per line). What
is the appropriate way to handle blank lines that hold whitespace, but
not characters? Currently, I'm doing this:

for user_file in user_files:
# Remove whitespace and make lowercase.
file_skip_list.append(user_file.strip().lower())

file_skip_list = list(sets.Set(file_skip_list))

However, if the input file has blank lines in it, I get this in my list:

'' (that's two single quotes with noting in between)

I thought I could do something like this:

if user_file == None:
pass

Or this:

if user_file == '':
pass

But, these don't work, the '' is still there. Any suggestions are
appreciated!
They are still there because you perform the stripping and lowercasing in
the append-call. Not beforehand. So change the code to this:

for uf in user_files:
uf = uf.strip().lower()
if uf:
file_skip_list.append(uf)

Diez
Jul 9 '07 #2
Diez B. Roggisch wrote:
They are still there because you perform the stripping and lowercasing in
the append-call. Not beforehand.
Thank you. I made the changes. It works.
Jul 9 '07 #3
Diez B. Roggisch <de***@nospam.web.dewrote:
...
for uf in user_files:
uf = uf.strip().lower()
if uf:
file_skip_list.append(uf)
This is fine; however, another reasonable alternative is:
if not uf.isspace():
file_skip_list.append(uf.strip().lower())

I prefer yours (it's simpler IMHO), but if I was doing a code review I
would accept either of these.
Alex
Jul 10 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: JDS | last post by:
Hi, all. I'd like to do the following, preferably *without* resorting to JavaScript: I have a long, dynamically-generated form questionnaire. Not all of the form fields are dynamically...
3
by: Generic Usenet Account | last post by:
I have written a small macro that provides the relative offset of any field within a structure. Here it goes: #define RELATIVE_OFFSET(a,b) \ { \ cout << "The relative offset of the " \ <<...
1
by: Claire | last post by:
I'm reading through some source code written by microsoft to show examples of porting serial comms to .net. Anyone who has written comms will know the following constants well. Can anyone...
13
by: orekin | last post by:
Hi There I have been programming C# for a couple of months and am trying to master Threading. I understand that ThreadPool uses background threads (see code example in MSDN page titled...
3
by: davidanthonyedwards | last post by:
Please Help, I have been given an Access application to have a look at, am newbie, etc. When I open the mdb file with the shift key down so that the menu options are available I can see all the...
9
by: Michael Redbourn | last post by:
Hi, I just switched from FP to DW and am very very happy ! So whilst I'm mastering Dreamweaver (gonna be a while yet :-) - I thought that I'd try and find out how to add borders for browers...
14
by: MLH | last post by:
After entering a date (earlier than today's date) into a textbox control though, I'm stumped as to why VBA thinks date I enter into the control is greater than Now - when they are CLEARLY less than...
1
oranoos3000
by: oranoos3000 | last post by:
hi i read below text and i understand what say but i dont identify meaning of this setence preg_match may not fail on 4 and 5 octet sequences, even though they are not supported by the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.