1

I'm new here and have a question. I do not have that much knowledge about programming because I am just a beginner, so I'd like to have as simple answers as possible. Of course, I will try my best understanding them! Also English is not my first language. Apologies for my poor English.

The task I want to do

I have a.txt contains 100-line data described by:

import numpy as np
b = np.arange(0.005, 0.05, 0.0001)
c = np.arange(1.5, 2.51, 0.01)

with open('a.txt','w') as f:
    for a in range(1,101):
        f.write('{:<3d} {:<3f} {:<3f}\n'.format(a,b[a-1],c[a-1]))

The data looks like this on a.txt:

1   0.005000 1.500000
2   0.005100 1.510000
3   0.005200 1.520000
4   0.005300 1.530000
5   0.005400 1.540000
6   0.005500 1.550000
7   0.005600 1.560000
8   0.005700 1.570000
....
97  0.014600 2.460000
98  0.014700 2.470000
99  0.014800 2.480000
100 0.014900 2.490000

Now, I want to pick and write only the 1st line through the 10th line's data into another text file, b.txt. How can I do that?

  • I'm dealing with a very small file for simplicity for now, but I want to do this task to a very large (like a few GB) text file in the future, so I'd like to know the way of doing the task which can also be used to deal with a huge file.

  • If there is any information which I do not show but is necessary, please let me know. I will add it as soon as possible.

I'd appreciate your help and your time. Thank you.

※Thank all of those who edited my post for doing that. It helped and will help me make my posts better.

Community
  • 1
  • 1
Violet
  • 13
  • 3

2 Answers2

2

First, you can get only the first n lines with itertools.islice, then write those lines:

from itertools import islice

n = 10

with open('a.txt', 'r') as infile, open('b.txt', 'w') as outfile:
    first_lines = islice(infile, n)
    outfile.writelines(first_lines)
iz_
  • 15,923
  • 3
  • 25
  • 40
  • Thank you for answering my question. I tried this code, and it perfectly worked. I didn't even know 'itertools.islice'. Thank you so much for helping me improve. I really appreciate it. – Violet Jan 13 '19 at 07:22
  • No problem. FYI, `islice` is essentially just list slicing (you know, that `mylist[start:end]` thing), except for iterators (such as files). – iz_ Jan 13 '19 at 07:29
  • Wow, then it's actually so simple! I still need time to get used to it, but it doesn't seem it takes so much time then! Thanks for additional info. – Violet Jan 13 '19 at 07:41
0

I grabbed this from the accepted answer to Read large text files in Python, line by line without loading it in to memory:

with open("log.txt") as infile:
    for line in infile:
        do_something_with(line)

Now, apply to your specific problem:

def grab_lines(in_path, out_path, start, end):
    with open(out_path, "w") as outfile:
        counter = -1
        with open(in_path) as infile:
            for line in infile:
                counter += 1
                if counter < start:
                    continue
                elif start <= counter <= end:
                    outfile.write(line)
                else:
                    return

Hope this helps!

duong_dajgja
  • 4,196
  • 1
  • 38
  • 65
  • Thank you for the answer and reference. Also thank you for showing a different way to do the task. I'm just curious, but does this code look simple and easy to read to you? I want my code to be easy for others to understand, so I'm seeking the simplest and easiest way. – Violet Jan 13 '19 at 07:30
  • @Violet It doesn't use any other libraries and it's flexible since you can do anything with an individual line with `do_something_with(line)`. – duong_dajgja Jan 13 '19 at 07:32
  • Oh that's true! We need import nothing from somewhere else. I will also try this idea. Thank you so much for helping me. – Violet Jan 13 '19 at 07:38