Please give an example. need a cycle with a postcondition. In which the array is read lines from a file, line by line. Array: mas_text = []
File from where it will be read: text_file
Thank.
Please give an example. need a cycle with a postcondition. In which the array is read lines from a file, line by line. Array: mas_text = []
File from where it will be read: text_file
Thank.
for line in file.xreadlines(): do_smth() post_condition()
UPD:
mas_text = [line for line in text_file.xreadlines()]
UPD2: but the beautiful version with the condition, but not the fact that this is what the topstarter needs:
mas_text = [post_condition(line) and line for line in text_file.xreadlines()]
Well, the post-condition can be made an anonymous function, but the EOT already need to specifically look at what you need to do.
map
. - VladDWhy do you need a post-condition to stop somewhere? then so, given the above:
# -*- coding: utf-8 -*- def file2list(text_file): try: f = open(text_file) except IOError: print "file %s not exist" %text_file return 1 #если просто до конца файла mas_text = f.readlines() # конец моего первого если # если при этом условия mas_text = [] for line in f.xreadlines(): if line == "начиная с этой не считывать\n": f.close() return mas_text mas_text.append(line) if line == "после этой не считывать\n": f.close() return mas_text # конец моего второго если f.close() return mas_text if __name__ == "__main__": mas_text = file2list("/tmp/file") print mas_text
Checked, at the end of lines it is necessary '\ n'
Source: https://ru.stackoverflow.com/questions/235923/
All Articles