我正在尝试在Python 3.6上读取一个文件并将其信息存储在两个不同的变量中,第一个存储来自样本#list的“样本”,另一个存储来自#list的“样本”。样本。但是我只从第一个列表和整个第二个列表中获得一行。
这就是我得到的:
第一个清单
ff44578jhT marsBug 2 7 3 5 2 1 71 235 312
第二个清单
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
这就是我应该得到的:
第一个清单
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
第二个清单
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
答案 0 :(得分:1)
最好将pandas与';'一起使用分离器:
SELECT D.[Date],
DATEPART(MONTH,D.[Date]) AS [Month],
CASE WHEN V.DD = 0 THEN 'Current Month'
WHEN V.DD = 1 THEN '1 Month Ago'
ELSE CONVERT(varchar(4), V.DD) + ' Months ago' END AS MonthsAgo
FROM [Date] D
CROSS APPLY (VALUES(DATEDIFF(MONTH, D.[Date], GETDATE()))) V(DD);
只需读入这两个文件,然后操纵数据框即可获得所需内容。
答案 1 :(得分:1)
根据您格式化的方式以及您尝试获取的内容,我会建议CSV module。不要担心,如果您使用冒号这种格式的大型列表,Python的csv模块也可以让您更改分隔符。
以下是您可以使用的一些代码:
import csv
with open('example.csv', 'rb') as csvfile:
reader = csv.reader(csvfile, delimiter='; ')
然后,为了获得每个读者对象的内容,basicaly函数作为列表。
print(reader[row][column])
这将打印行和列的值。您可能必须为文件创建标题才能在Python中使用。查看Python文档以获取更多信息。
答案 2 :(得分:1)
正如@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)
输出:
Examples:
['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']
Samples:
['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']