每隔第一行,第二行和第三行读取一个txt文件,并保存在3个不同的列表中

时间:2015-08-31 15:37:29

标签: python iteration text-files

我有一个巨大的常量格式文本文件,如下所示:

first line
2nd line
3rd line 
#Mi Mj
#Ni Nj Nk
#Pi Pj
#----------- The numeric values start here ------
M0 M1
N0 N1 N2
P0 P1
M1 M2
N1 N2 N3
P1 P2
M2 M3
N2 N2 N3
P2 P3
...

我需要跳过第1行到第7行,然后在第1行,第2行和第3行之后读取文件,然后将其保存到三个不同的列表中。

我设法通过跳过其他2个来为我的代码中的每一行执行此操作,但不是每个第2行和第3行。如何使用下两次(fileobject)并不是一种有效的方法。所以任何人都可以告诉我如何才能最好地处理大文件? 我最后需要这样的结果:

list1 = [M0, M1, M1, M2, M2, M3]
list2= [N0,N1,N2,N1, N2,N3,N2,N2, N3]
list3= [P0, P1, P1, P2,P2, P3] 

这是我的代码:

    # Python 3
myfile = open('myfile.txt', 'r')

m,n,p = [], [], []

for line in myfile:
    ll = line.strip()  # string
    if not ll.startswith("#"):
        row = ll.split()  # list
        print(row)
        try:
            m.append(row[0]) # append first column of every third line
        except IndexError:
            print('There is not a standard line: ', line)
        next(myfile)
        next(myfile)
print(m)
myfile.close()

4 个答案:

答案 0 :(得分:0)

这样的事情应该有效:

    let fontFamily = UIFont.systemFontOfSize(UIFont.systemFontSize()).familyName
    print(fontFamily)

    if let attributedString = attributedStringFromDescription(originalDescription, withFontName: fontFamily) {
        myLabel.attributedText = attributedString
    }

import itertools from collections import defaultdict result = defaultdict(list) counter = 0 with open('myfile.txt') as fh: for line in itertools.islice(fh, 7, None): row = line.strip().split() result[counter % 3].extend(row) counter += 1 将是一个字典(defaultdict),其中包含result01个键,其中包含与您2,{{1}对应的列表}和m。当然你可以改变它,我只是为了避免代码重复。

答案 1 :(得分:0)

我假设这里的绊脚石是你在循环命令中读取行并努力分离m n和p。如果是这种情况,那么我建议在开始循环之前从读取文件中获取总行数并使用范围函数-7 / 3(这将需要非常严格地遵守您的文本文件结构,但是允许您在每个循环实例中切换三行。请参阅下面的代码。

package com.example.dltests;

import java.net.HttpURLConnection;
import java.net.URL;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView mTextView = (TextView) findViewById(R.id.hello);

        try 
        {
            URL mUrl = new URL("http://192.168.1.53:8080/some.zip");
            HttpURLConnection mHttpURLConnection = (HttpURLConnection) mUrl.openConnection();
            mTextView.setText(mHttpURLConnection.getContentLength());

        } 
        catch (Exception e) 
        {
            mTextView.setText("ERROR: " + e.getMessage());
        }

    }
}

希望这会有所帮助。您必须添加错误检查,我为了简单起见将其删除。此外,我还没有编译代码,因此可能存在语法错误。修复的骨头就在那里。

请注意,您可以使用以下方法剪切读取线并将线条分割为一行:

myfile = open('myfile.txt', 'r')

m,n,p = [], [], []


# Obtain line count from text file and work out iterations needed for loop

myfile_length = len(myfile.readlines())
iterations_needed = ((myfile_lenght-7)/3)
my_file.seek(0)


for i in range(7):   # Gets past those pesky first 7 lines
   myfile.readlines()


for x in range(iterations_needed):

    ll = my_file.readline()  # string  
    row = ll.split()  # list
    m.append(row[0]) # append first column of every third line
    m.append(row[1])

    ll = my_file.readline()  # string
    row = ll.split()  # list
    n.append(row[0]) 
    n.append(row[1])
    n.append(row[2])

    ll = my_file.readline()  # string
    row = ll.split()  # list
    p.append(row[0]) 
    p.append(row[1])


print(m)
print(n)
print(p)

myfile.close()

答案 2 :(得分:0)

我认为,你思考的基本缺陷是你要读取所有第一行数字,然后是第二行。最好一次读取文件,而不是在读取文件时整理出不同的行。一种方法是在另一个答案中使用行计数器mod 3,或者你可以继续读取for循环中的行,如下所示:

myFileList= """first line
2nd line
3rd line
#Mi Mj
#Ni Nj Nk
#Pi Pj
#----------- The numeric values start here ------
M0 M1
N0 N1 N2
P0 P1
M1 M2
N1 N2 N3
P1 P2
M2 M3  
N2 N2 N3
P2 P3"""

myFakeFile = iter(myFileList.split('\n'))

m,n,p = [], [], []
m2, n2, p2 = [], [], []

# Skip the first few lines
for skip_lines in range(7):
    next(myFakeFile)

# Read the next line ...
for line in myFakeFile:

     # ... and the following two lines
    second_line = next(myFakeFile)
    third_line = next(myFakeFile)

    # Either append line directly, 
    m.append(line)
    n.append(second_line)
    p.append(third_line)

    # or split the lines before appending
    m2.extend(line.split())
    n2.extend(second_line.split())
    p2.extend(third_line.split())


print('m = {}'.format(m))
print('n = {}'.format(n))
print('p = {}\n'.format(p))

print('m2 = {}'.format(m2))
print('n2 = {}'.format(n2))
print('p2 = {}\n'.format(p2))

我有点不确定你是否想再次分割实际的行,所以我添加了显示它的m2, n2, p2列表。我不得不伪造文件以获得一个运行的例子。但是这段代码会产生以下输出:

m = ['M0 M1', 'M1 M2', 'M2 M3']
n = ['N0 N1 N2', 'N1 N2 N3', 'N2 N2 N3']
p = ['P0 P1', 'P1 P2', 'P2 P3']

m2 = ['M0', 'M1', 'M1', 'M2', 'M2', 'M3']
n2 = ['N0', 'N1', 'N2', 'N1', 'N2', 'N3', 'N2', 'N2', 'N3']
p2 = ['P0', 'P1', 'P1', 'P2', 'P2', 'P3']

答案 3 :(得分:0)

fh = open('in.txt', 'r')

list1, list2, list3 = [], [], []


def get_list(num):
    return {
        0: list3,
        1: list1,
        2: list2,
    }[num]

count = 1

for i, line in enumerate(fh, 1):

    if (i < 7):
        continue

    get_list(count % 3).extend(line.rstrip().split())
    count += 1

print("{}\n{}\n{}".format(list1, list2, list3))