I am reading lines from an input file and split each of the line into list. However, I encountered the following situation that confused me.
This is my code:
with open("filename") as in_file:
for line in in_file:
print re.split(r'([\s,:()\[\]=|/\\{}\'\"<>]+)', line)
This is the demonstration of my input file:
PREREQUISITES
CUDA 7.0 and a GPU of compute capability 3.0 or higher are required.
Extract the cuDNN archive to a directory of your choice, referred to below as <installpath>.
Then follow the platform-specific instructions as follows.
And this is the output results I got:
['PREREQUISITES', '\n', '']
['', '\n', '']
['', ' ', 'CUDA', ' ', '7.0', ' ', 'and', ' ', 'a', ' ', 'GPU', ' ', 'of', ' ', 'compute', ' ', 'capability', ' ', '3.0', ' ', 'or', ' ', 'higher', ' ', 'are', ' ', 'required.', '\n', '']
['', '\n', '']
['', '\n', '']
['', ' ', 'Extract', ' ', 'the', ' ', 'cuDNN', ' ', 'archive', ' ', 'to', ' ', 'a', ' ', 'directory', ' ', 'of', ' ', 'your', ' ', 'choice', ', ', 'referred', ' ', 'to', ' ', 'below', ' ', 'as', ' <', 'installpath', '>', '.', '\n', '']
['', ' ', 'Then', ' ', 'follow', ' ', 'the', ' ', 'platform-specific', ' ', 'instructions', ' ', 'as', ' ', 'follows.', '\n', '']
My questions are:
Q1: At the end of each line, besides of the character \n
, there is an another empty element ''
. What is that?
Q2: esides of the first, all the other lines are starting with this empty element ''
. Why is that?
Edit:
Added question Q3: I want the the delimiters such as ' '
and '\n'
kept in the results but not this empty emement ''
. Is there any way to do this?
Answer to question Q1-2: here.
Answer to question Q3: here.