简体   繁体   中英

read every 5th line in a data file and append to a new list

I am trying to seperate every 5th line to append to a new file, the first line of each five line has the data I need, this is the only way that I have not had any errors but the output when I print the second line (as a test) of the new datafile which should be the sixth line of my original file, returns the second line of the original file and the len of my new data should only be around 2400 line not 202571 lines. Once I get just the line needed I can get the data to put into a kml file.

dataList = []
line_count = 0
for line in lineList:
    line_count+= 5
    dataList.append(line)
    line_count += 5
print (format(len(dataList)))
print (dataList [1])



202571
M010176A         B:  0    0   0 S:  0    0   0 M: 12   30 135 CMT: 1 BOXHD:          9.4

I think you are using wrong logic while iterating loop. Your code looks like following.

dataList = []
line_count = 0
for line in lineList:
    line_count+= 5
    dataList.append(line)
    line_count += 5

In above code, you are appending every line to dataList, no matter what! Because there is no logic to check if it's fifth line and then append to a list.

Following changes may let you achieve your goal.

line_count = 4
for line in lineList:
    line_count += 1
    if line_count == 5:
        line_count = 0
        dataList.append(line)

Rest of your code will work fine after applying these changes. Hope it helps!

Here's an easy way to do it: It will iterate through all the lines and check to see if the line is divisible by 5 , hence the % 5 . If you want it to start at 0, simply remove the + 1 . If you are just trying to output this to a file, I think it would be better to simply speed up the process by writing as you read the other file. If you want me to, I can modify it back, just comment if you need any help.

dataList = []
with open("in.kml", "r") as fi:
    for i, line in enumerate(fi):
        if (not (i % 5)):
            dataList.push(line)

If you just want to output every fifth line, then the Python islice() function will help you as follows:

from itertools import islice

with open('input.txt') as f_input, open('output.kml', 'w') as f_output:
    for fifth in islice(f_input, 0, None, 5):
        f_output.write(fifth)

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