3

I have this code to print some strings to a text file, but I need python to ignore every empty items, so it doesn't print empty lines.
I wrote this code, which is simple, but should do the trick:

lastReadCategories = open('c:/digitalLibrary/' + connectedUser + '/lastReadCategories.txt', 'w')
for category in lastReadCategoriesList:
    if category.split(",")[0] is not "" and category is not None:
        lastReadCategories.write(category + '\n')
        print(category)
    else: print("/" + category + "/")
lastReadCategories.close()

I can see no problem with it, yet, python keeps printing the empty items to the file. All categories are written in this notation: "category,timesRead", that's why I ask python to see if the first string before the comma is not empty. Then I see if the whole item is not empty (is not None). In theory I guess it should work, right?
P.S.: I've already tried asking the if to check if 'category' is not "" and is not " ", still, the same result.

12
  • 5
    is not "" is suspect. is checks for object identity, not object equality. Try if ... != "" Commented May 21, 2013 at 21:46
  • 1
    You are looping over lastReadCategoriesList; what is it's definition? Commented May 21, 2013 at 21:46
  • it is a list with items written like "category,timesRead", in which "timesRead" is an integer made into string. For some reason I still can't put my finger on, some empty items are being added to the list. Commented May 21, 2013 at 21:49
  • @millimoose, It still adds the empty items, even using != "" and != " " Commented May 21, 2013 at 21:53
  • 1
    @millimoose is not "" will work in CPython, but that's a quirk of the interpreter and not a part of the language spec. Here's some more info: stackoverflow.com/questions/1392433/… Commented May 21, 2013 at 22:06

3 Answers 3

4

Test for boolean truth instead, and reverse your test so that you are certain that .split() will work in the first place, None.split() would throw an exception:

if category is not None and category.split(",")[0]:

The empty string is 'false-y', there is no need to test it against anything.

You could even just test for:

if category and not category.startswith(','):

for the same end result.

From comments, it appears you have newlines cluttering up your data. Strip those away when testing:

for category in lastReadCategoriesList:
    category = category.rstrip('\n')
    if category and not category.startswith(','):
        lastReadCategories.write(category + '\n')
        print(category)
    else: print("/{}/".format(category))

Note that you can simply alter category inside the loop; this avoids having to call .rstrip() multiple times.

Sign up to request clarification or add additional context in comments.

Comments

1

rstrip() your category before writing it back to file

lastReadCategories = open('c:/digitalLibrary/' + connectedUser +'/lastReadCategories.txt', 'w')
for category in lastReadCategoriesList:
if category.split(",")[0] is not "" and category is not None:
    lastReadCategories.write(category.rstrip() + '\n')
    print(category.rstrip())
else: print("/" + category + "/")
lastReadCategories.close()

I was able to test it with your sample list provided (without writing it to file):

lastReadCategoriesList =  ['A,52', 'B,1\n', 'C,50', ',3']
for category in lastReadCategoriesList:
if category.split(",")[0] is not "" and category is not None:
    print(category.rstrip())
else: print("/" + category + "/")

>>> ================================ RESTART ================================
>>> 
A,52
B,1
C,50
/,3/
>>> 

5 Comments

It stoped printing the empty lines... Except for the one in "B,1\n". I mean, when I print it to the shell, it prints exactly like yours, but not on the file. Do you have any idea why that happens? (also, I added the rstrip() to the part of the code that generates the list and asked it to print the list when it was done generating it. It still printed "B,1\n")
Can you give me the exact list?
@AugustoQ did you change this part lastReadCategories.write(category.rstrip() + '\n')?
@AugustoQ just tested with writing to file... works like a charm.
Yes, and that is why I'm finding it really strange, cause it makes no sense at all that it shoul print "B,1\n" (everything else is solved already)
0

The classic way to test for an empty string (ie, only whitespace but not '') is with str.strip():

>>> st='   '
>>> bool(st)
True
>>> bool(st.strip())
False

Which also works on a null string:

>>> bool(''.strip())
False

You have if category.split(",")[0] is not "" ... and this is not the recommended way. You can do this:

if category.split(',')[0] and ...

Or, if you want to be wordier:

if bool(category.split(',')[0]) is not False and ...

And you may be dealing with an issue with leading whitespace in the CSV:

>>> '    ,'.split(',')
['    ', '']
>>> '     ,val'.split(',')
['     ', 'val'] 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.