Why is stripping a newline character in Python creating a blank space as a list item?

StackOverflow https://stackoverflow.com/questions/22519589

  •  17-06-2023
  •  | 
  •  

I have the following code:

# File declaration.

infileS = open("single.dat", 'r')
infileD = open("double.dat", 'r')
infileT = open("triple.dat", 'r')
infileHR = open("homerun.dat", 'r')
infileAB = open("atbat.dat", 'r')
infileP = open("player.dat", 'r')


# Fill up the lists.
single = infileS.read()
double = infileD.read()
triple = infileT.read()
homerun = infileHR.read()
atbat = infileAB.read()
player = infileP.read()

single = [item.rstrip() for item in single]
double = [item.rstrip() for item in double]
triple = [item.rstrip() for item in triple]
homerun = [item.rstrip() for item in homerun]
atbat = [item.rstrip() for item in atbat]
player = [item.rstrip() for item in player]

print (single)

What prints:

['5', '', '3', '', '1', '0', '', '1', '2', '', '6', '', '9', '', '2', '0', '', '4', '', '7']

I don't want the '' items. What have I done wrong and what can I do to fix this?

All the .dat files are simple Notepad lists of numbers. The "single.dat" is a list of numbers with "enter" putting them on different lines (with no lines in between), and looks like: (minus, of course, the spaces between the paragraphs containing those numbers)

5 

3 

10 

12 

6 

9 

20 

4 

7 
有帮助吗?

解决方案

The empty strings ('') are what's left over if you strip something that's all whitespace (or possibly they were empty to start with). The easiest way to eliminate these is to use the fact that '' is falsy, so you can remove them right there in your list comprehensions by adding if item.strip().

The problem is that you're iterating over the output of file.read(), which is a single string. Strings in Python are iterable, but this means that when you iterate over them, you iterate over each character. So what you're doing is stripping each individual character and adding it to your list--so all your newlines turn into empty strings, rather than being stripped out like I think you intended.

To fix it, use the fact that file objects are also iterable, and iterate line-by-line. This is the idiomatic way to read a file line-by-line in Python (using a context manager rather than a lone open call):

with open('single.dat') as f:
    for line in f:
        dosomething(line)

So, use that pattern along with some filtering in your list comprehension, and you'll be all set:

with open('single.dat') as f:
    single = [line.strip() for line in f if line.strip()]

其他提示

It might be easiest to just filter out the ''. For instance:

>>> list = ['', 'cat', 'dog', '']
>>> filter(None, list)
['cat', 'dog']
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top