我如何只打印每5行

时间:2016-12-11 02:57:40

标签: python printing lines

我有一个文本文件("name_data.txt"),其中包含以下内容:

name: Kelo
family name: Lam
location: Asia
members: Kelo, Kiko, Jil

name: Miko
family name: Naiton
location: Japan
members: Miko,Kayati 

文本文件使用相同的模式(名称,姓氏,位置,成员)

我想打印出第一行,然后每隔5行打印一次,所以我只会在开头打印带有“name”的行。 然后我想要一个名单列表

我希望我的输出为:

["Kelo","Miko"]

到目前为止,我已经得到了(虽然这是错误的):

name_data= load_local_file('name_data.txt',ignore_header=False,delimiter='\t')


def __init __(name_reader): 

    names=list()  
    count=0  
    name_line=5  
    line_number=0  

    for name in name_data:

        if line_number<5:  

            line_number +=1  

        if line_number ==5: 

            names.append(line_number)  

6 个答案:

答案 0 :(得分:4)

您可以通过将 [DataType(DataType.Time)] public TimeSpan StartTime { get; set; } [DataType(DataType.Time)] public TimeSpan EndTime { get; set; } [DataType(DataType.Date)] [Column(TypeName = "Date")] public DateTime StartDate { get; set; } [DataType(DataType.Date)] [Column(TypeName = "Date")] public DateTime EndDate { get; set; } 与数字进行比较来识别每五行。在你的情况下,这应该是linenumber modulo 5,因为你想要第一行和第6行,第11行......(注意python从索引0开始)

要获取行号和内容,您可以使用enumerate迭代文件。

然后丢弃字符串的0部分并保留之后的内容,您可以使用str.split()

工作实现可能如下所示:

name:

您可以使用带有步骤参数的itertools.islice代替枚举:

# Create an empty list for the names
names = []

# Opening the file with "with" makes sure it is automatically closed even
# if the program encounters an Exception.
with open('name_data.txt', 'r') as file:
    for lineno, line in enumerate(file):
        # The lineno modulo 5 is zero for the first line and every fifth line thereafter.
        if lineno % 5 == 0:
            # Make sure it really starts with "name"
            if not line.startswith('name'):
                raise ValueError('line did not start with "name".')
            # Split the line by the ":" and keep only what is coming after it.
            # Using `maxsplit=1` makes sure you don't run into trouble if the name 
            # contains ":" as well (may be unnecessary but better safe than sorry!)
            name = line.split(':', 1)[1]
            # Remove any remaining whitespaces around the name
            name = name.strip()
            # Save the name in the list of names
            names.append(name)

# print out the list of names
print(names)

根据您的需要,您可以考虑使用from itertools import islice with open('name_data.txt', 'r') as file: for line in islice(file, None, None, 5): ... # like above except for the "if lineno % 5 == 0:" line 模块来完全解析文件:

re

对于您的示例,import re # The regular expression group = re.compile(r"name: (.+)\nfamily name: (.+)\nlocation: (.+)\nmembers: (.+)\n", flags=re.MULTILINE) with open(filename, 'r') as file: # Apply the regex to your file all_data = re.findall(group, file) # To get the names you just need the first element in each group: firstnames = [item[0] for item in all_data] 将为firstnames,如果您使用['Kelo', 'Miko'],则[item[1] for item in all_data]会得到姓氏:['Lam', 'Naiton']。 要成功使用正则表达式,您必须确保它与您的文件布局完全匹配,否则您将得到错误的结果。

答案 1 :(得分:2)

执行此操作的简单方法如下:

with open('name_data.txt', 'r') as file:

    index = 0
    for line in file:
        if index % 5 == 0:
            print(line.split()[1])
        index += 1

答案 2 :(得分:2)

您可以使用列表推导

在一行中执行此操作
c = open('test.txt', 'r').readlines()

# for every fifth line extract out name and store in list
a = [i.replace('name: ', '').replace('\n', '') for i in c[::5]]

print(a) # ['Kelo', 'Miko']

答案 3 :(得分:1)

假设name_data是文件中的行列表,您可以执行

names = []
for i in range(1, len(name_data), 5):
    names.append(name_data[i].split(":")[1].strip())

答案 4 :(得分:0)

拥有包含以下数据的name_data.txt文件: 1 2 3 4 5 6 7 8 9 10

以下是打印第一行和第五行的方法:

content = [line.rstrip('\n') for line in open('name_data.txt')]
names = []
limit = 4
fp = open("name_data.txt")
names.append(content[0])
for i, line in enumerate(fp):
    if i == limit:
        names.append(line)
        limit += 5
fp.close()
print(names)

结帐http://shortcode.pro/code/read-txt-file-and-print-first-and-every-5th-line/

答案 5 :(得分:0)

您可以使用正则表达式 - Python的模块是re

然后name_data.txt为:

name: Kelo
family name: Lam
location: Asia
members: Kelo, Kiko, Jil

name: Miko
family name: Naiton
location: Japan
members: Miko,Kayati 

获取名称很简单:

import re

def get_names():

    with open('name_data.txt', 'r') as f:
        print(re.findall(r'^name:\s*(\w+)', f.read(), flags=re.MULTILINE))

if __name__ == '__main__':

    get_names()

请注意多行标记设置 - 当设置为全局时,正则表达式也会匹配family name: ...行。 以交互模式here查看正则表达式。