使用python在文本文件中打印一页

时间:2017-10-24 05:54:46

标签: python file

我对python很安静,我想从文本文件中提取一页。每个页面都以唯一的一行开头,以' //'结尾。我想找到一种方法来返回某个页面,知道与之相关的唯一行。你知道我可以用什么工具吗?

3 个答案:

答案 0 :(得分:1)

您可以source = [] for key in tr: source = key[0] for value in sr: if value[0] not in source: print (value[0], value[1], value[2]) target = [] for key in sr: target = key[0] # print (target) for value in tr: #print (value[0]) if value[0] not in target: print (value[0], value[1], value[2]) common = [] for key in sr: common = key[0] # print (target) for value in tr: #print (value[0]) if value[0] in common: print (value[0], value[1], value[2]) split文件,然后"//" split获取第一行,然后创建将一行与页面相关联的字典。

"\n"

答案 1 :(得分:0)

您可以加载文本文件并使用拆分作为“//”

file = open(“testfile.txt”, “r”) 
contents = file.read() 
pages = contents.split("//")

页面现在是一个列表,您可以指出要获取的索引

print(pages[0])

答案 2 :(得分:0)

您可以尝试将文本拆分为列表:

import pickle

text= "hello this is a unique line. and it will end with // this should be 
the other line and it also ends with // a third line just for good mesure//"
pickle.dump(text, open("text.txt","wb"))

page = pickle.load(open("text.txt","rb")).split("//")[0]

print(page)

然后只需在此行中为您想要的页面编制索引:

 page = pickle.load(open("text.txt","rb")).split("//")[0]