我试图读取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
最好将pandas与“;”分隔符一起使用:
只需读入这两个文件,然后操作数据帧以获得所需的内容。在
根据你格式化的方式和你想要得到的东西,我建议使用CSV module。不用担心,如果您有这种格式的带有冒号的大列表,Python的csv模块也可以让您更改分隔符。在
下面是一些您可以使用的代码:
然后,为了得到每一个的内容,reader对象basicaly函数作为一个列表。在
^{pr2}$它将打印行和列的值。您可能需要为文件创建头才能在Python中工作。查看Python文档以获取更多信息。在
正如@Preston Hager提到的,文件的格式建议您使用
csv
模块。但是,另一种不使用csv
并从.txt
文件读取的方法是:输出:
^{pr2}$相关问题 更多 >
编程相关推荐