The pandas read_table()
function enables us to read *.tab
file and the parameter skiprow
provides flexible ways to retrieve the data. However, I’m in trouble when I need to read *.tab
file in a loop but the number of the rows need to skip is random. For example, the contents need to skip are started with /*
and ended with */
, such as:
JavaScript
x
6
1
/*
2
...
3
The number of rows need to skip is random
4
...
5
*/
6
So how do I find the line of the */
and then use the parameter skiprow
?
Advertisement
Answer
Consume rows until the current row starts with '*/'
:
JavaScript
1
5
1
with open('data.txt') as fp:
2
for row in fp:
3
if row.startswith('*/'):
4
df = pd.read_table(fp)
5