0

ihave a file containing data in the form of table with 5 columns and 10 rows the columns are separated by spaces . i have a to read the file line by line without reading the first line which is the header line.Please suggest me the code using python technology

3
Contributors
3
Replies
4
Views
4 Years
Discussion Span
Last Post by vimalya
-1

enjoy:

f = open('C:/data.txt', 'rt')
a = f.read().split('\n')
f.close()
del a[0]
del a[10:]
print a
1

enjoy:

Not so much to enjoy in that code.
You dont read file line by line as @vimalya mention.
With read() you read all file into memory.
del we almost never need to use in python.

Example.
data.txt-->

header
1 2 3
4 5 6
7 8 9

---

with open('data.txt') as f:
    line = f.next()
    for line in f:
        #print line
        #You can procces data like this 
        print [int(item) for item in line.split()]

"""Output-->
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
"""

Next time vimalya try to post some code,not just a task to solve.

Edited by snippsat

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.