如何在python3.6中读取文件并分离其内容

2024-09-26 22:08:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图读取Python3.6上的一个文件,并将他的信息存储在两个不同的变量上,第一个变量存储示例列表中的“示例”,另一个存储示例列表中的“示例”。但是我只从第一个列表和整个第二个列表中得到一行。在

我正在阅读的文件:

我得到的是:

First list

ff44578jhT marsBug 2 7 3 5 2 1 71 235 312

Second list

k345fv78 littleMonster 2 4 3 0 2 1 89 2345 0

k434fv78 bigMonster 1 3 3 0 2 1 89 2345 0

k623fv78 hugeMonster 2 4 3 0 2 1 89 2345 0

k13ued31 edu 3 2 1 8 0 1 20 4 0

k123vv31 notbigMonster 4 8 9 3 4 2 200 4000 0

这就是我应该得到的:

First list

ff44578jhT marsBug 2 7 3 5 2 1 71 235 312

ff11443asT; momu; 4; 2; 1; 4; 6; 3; 1; 11; 23

ff1123dasT; nomu; 1; 3; 1; 2; 3; 2; 1; 1; 3

ff44578jhT; jupiterBug; 2; 7; 3; 5; 2; 1; 71; 235; 312

ff44578jhT; uranusBug; 2; 7; 3; 5; 2; 1; 71; 235; 312

k123vv31; bibug; 4; 8; 9; 3; 4; 2; 200; 4000; 0

Second list

k345fv78 littleMonster 2 4 3 0 2 1 89 2345 0

k434fv78 bigMonster 1 3 3 0 2 1 89 2345 0

k623fv78 hugeMonster 2 4 3 0 2 1 89 2345 0

k13ued31 edu 3 2 1 8 0 1 20 4 0

k123vv31 notbigMonster 4 8 9 3 4 2 200 4000 0

def readFromFile(file_name):
    examplars=[]
    samples=[]
    in_file = open(file_name, 'r')

    if "#List of exemplars:\n" in in_file:
        for line in in_file:
            info1, info2, info3, info4, info5, info6, info7, info8, info9, info10, info11 = line.split("; ")
            print(info1, info2, info3, info4, info5, info6, info7, info8, info9, info10, info11) #using print to see what is happening but the objective would be to append all the infos in a tuple
            if "#List of samples:\n" in in_file:
                    for line in in_file:
                        info1, info2, info3, info4, info5, info6, info7, info8, info9, info10, info11 = line.split("; ")
                        print(info1, info2, info3, info4, info5, info6, info7, info8, info9, info10, info11) #using print to see what is happening but the objective would be to append all the infos in a tuple

Tags: in示例列表linelistfileinfo2info5
3条回答

最好将pandas与“;”分隔符一起使用:

import pandas as pd
df = pd.read_csv('file_name.txt', separator = ';', header=None)

只需读入这两个文件,然后操作数据帧以获得所需的内容。在

根据你格式化的方式和你想要得到的东西,我建议使用CSV module。不用担心,如果您有这种格式的带有冒号的大列表,Python的csv模块也可以让您更改分隔符。在

下面是一些您可以使用的代码:

import csv
with open('example.csv', 'rb') as csvfile:
    reader = csv.reader(csvfile, delimiter='; ')

然后,为了得到每一个的内容,reader对象basicaly函数作为一个列表。在

^{pr2}$

它将打印行和列的值。您可能需要为文件创建头才能在Python中工作。查看Python文档以获取更多信息。在

正如@Preston Hager提到的,文件的格式建议您使用csv模块。但是,另一种不使用csv并从.txt文件读取的方法是:

with open("examples.txt", "r") as inFile:

    #Read all data from file.
    data = inFile.read()

    #Split each set to examples and samples.
    examples = data.split("#")[1].split(":\n")[1].split("\n")
    samples = data.split("#")[2].split(":\n")[1].split("\n")

    #Create sublists of every example or sample record and dispose the last record which is empty.
    examples = [example.split(";") for example in examples][:-1]
    samples = [sample.split(";") for sample in samples][:-1]

    #Print results.
    print("Examples: ")
    for example in examples:
        print(example)

    print("Samples: ")
    for sample in samples:
        print(sample)

输出:

^{pr2}$

相关问题 更多 >

    热门问题