0

I wrote a function which takes a .txt file.

The first thing it does is split the file at ',' and add them to a list, which creates a list of lists.I used:

lst = s.split(',')

I do get a list of lists, except every second line has an empty list ['\n']. I need to find a way get rid of these empty lists as they muck up the rest of the code.

Is there any simple way of doing this? Or is it just that I doing something wrong?

Any help would be greatly appreciated. Thank You!

Sample Data:

1,2,3,4
,3,4,

Expected Output:

['1','2','3','4\n']
['','3','4','\n']

Current Output:

['1','2','3','4\n']
['\n']
['','3','4','\n']

Output after using sshashank124's suggestion:

['1','2','3','4\n']
[]
['','3','4','\n']

Output after using Alex Thornton's suggestion:

['1','2','3','4\n']
[]
['','3','4','\n']
DejaVu
  • 23
  • 1
  • 6

4 Answers4

1

Use strip() (or rstrip()) to get rid of new-line characters:

lst = s.strip().split(',')

See also: How can I remove (chomp) a newline in Python?

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • 1
    This does delete the '\n'. However, it does not delete the list it self the contains the '\n' – DejaVu Apr 16 '14 at 15:17
0

You can simply do it as:

lst = [i for i in s.split(',') if i != '\n']

Example

>>> s = 'hello,\n,world,\n,bye,\n,world'

>>> lst = [i for i in s.split(',') if i != '\n']
>>> print lst
['hello', 'world', 'bye', 'world']
sshashank124
  • 31,495
  • 9
  • 67
  • 76
  • This removes the string insides the list but the list remains there. So I still have the [] hanging after the previous list. Is there anyway to delete this aswell? – DejaVu Apr 16 '14 at 15:22
0
lst = filter(lambda a: len(a)==1 and a[0]!='\n' or len(a)>1, lst)

This will clear the list of empty list [ ] or list containing only \n. But it's probably better to clear the text of unwanted strings BEFORE splitting it to a list.

Edit

note at the end of code you have to replace the lst with your list containing the unwanted characters. also you could try removing the \n from the original text file with this code.

text_file = text_file.replace('\n', '')
jibreel
  • 359
  • 2
  • 8
  • This is giving me an error, but is there anyway to do something before hand to stop the '\n' from coming up? – DejaVu Apr 16 '14 at 15:58
  • @Alex Thornton Yea it still leaves the [] in :/ – DejaVu Apr 16 '14 at 16:37
  • @Alex Thornton Still the same, I can't seem the figure out how to get rid of the []. – DejaVu Apr 16 '14 at 17:14
  • @DejaVu could you show the full code that you are using to test the solutions in your question? I will be able to update my answer and hopefully all will be fine and dandy. Ping me on my answer to let me know when you've done it. – anon582847382 Apr 16 '14 at 17:19
0

If your sample data doesn't have a blank line between the two populated lines, then it seems to me you are having a line-end issue upon reading the text file in the first place. If you post how you're reading in the text file, you could get answers that fix your problem before it ever gets to the point where you have any empty lists to remove.

That said, if lst really does just have [\n] as every other element, you can simply skip them as follows:

lst = s.strip().split(',')[::2]

(Note I've already incorporated the strip mentioned in previous answers to remove the newline characters.)

John Y
  • 14,123
  • 2
  • 48
  • 72