简体   繁体   中英

How to read lines from file starting from arbitrary newline in Python

I have a file formatted in a way that lines are separated with a new line, like the following

1 1 1 1 
2 2 2 2 2 2 2
3 3 3 3 3 3

I would like to read the lines separately starting, for example, from the second one and save them in an array. I think I can manage the last part, but I can't figure out how to read starting from the nth newline of the file. Any idea on how can I do it? Thanks. Best regards.

As files are iterable in python you could call next on it to skip the first line, for example:

with open('data.txt', 'r') as data:
    next(data)
    for line in data:
        print line.split()

Would yield:

['2', '2', '2', '2', '2', '2', '2']
['3', '3', '3', '3', '3', '3'] 

References:

You can use itertool.islice for this, eg:

from itertools import islice

with open('filename') as fin:
    wanted = islice(fin, 1, None) # change 1 to lines to skip
    data = [line.split() for line in wanted]
lines = open('test.txt', 'r').readlines()

# n is your desired line
for lineno in range(n-1, len(lines)):
    print list(lines[lineno].strip())

Well, you could do something like this:

n1, n2 = 0, 2

with open('filename.txt') as f:
    print '\n'.join(f.read().split('\n')[n1:n2+1])

This would produce (as per the contents in the file you've posted) the output like this:

1 1 1 1 
2 2 2 2 2 2 2
3 3 3 3 3 3

EDIT 1 :

@mic-tiz According to the comment you posted below, I understand that you wish to have all the numbers in your text file into a single array.

with open('filename.txt') as f:
    array = [i for i in f.read() if not i == ' ']

This code as you mentioned, would produce a list array

array = ['1', '1', '1', '1', '\n', '2', '2', '2', '2', '2', '2', '2', '\n', '3', '3', '3', '3', '3', '3']

Then, you can print the elements by splitting it on the occurrence of \\n character.

EDIT 2 : You can save those numbers in a dictionary using the code below

d = {}
with open('filename.txt') as f:
    array = f.read().split('\n')

for i in range(len(array)):
    d['l%r'%i] = [int(j) for j in array[i] if not j == ' ']

This will produce d = {'l2': [3, 3, 3, 3, 3, 3], 'l0': [1, 1, 1, 1], 'l1': [2, 2, 2, 2, 2, 2, 2]}

You cannot jump directly to a specific line. You have to read the first n lines:

n = 1
with open('data.txt', 'r') as data:
    for idx, _ in enumerate(data):
        if idx == n:
            break
    for line in data:
        print line.split()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM