with open(filename) as f:
file_list = f.readlines()
file_list = [line.strip() for line in file_list] # remove whitespaces from each line of file
code to process data between start and end tags (these tags can have whitespaces thats why i have removed them above)
이 코드는 나를을 위해 잘 작동하지만, 파일이 너무 큰 경우 다음 난에 전체 데이터를 복사하기 위해이 현명한 생각하지 않는다 목록에서 각 줄의 공백을 제거하십시오.
목록의 특정 부분에 대한 공백을 제거하여 목록에 저장할 수있는 부분을 많이 줄일 수 있습니까?
내가 시도 :
with open(filename) as f:
for line in f.readlines():
if line.strip() == "start":
start = f.readlines.index("start")
if line.strip() == "end"
end = f.readlines.index("end")
file_list = f.readlines[start:end]
을하지만 그것주는 오류
start = f.readlines.index("start")
AttributeError: 'builtin_function_or_method' object has no attribute 'index'
난 그냥이 게시물의 상단에 언급 한 코드의 효율적인 코드를 작성합니다.
file : 읽을 줄이 더 이상 남아 있지 않습니다. 그러나 도움이 필요하면 사람들에게 문제가 무엇인지 알려주려고 노력해야합니다. – nekomatic
'file_list = f.readlines(). strip()''이 코드는 잘 작동합니다. '- 정말요? 나는 지난번에 당신이 목록을 '스트립 (strip)'할 수 없다는 것을 확인한 이래로 이것이 오류라고 생각했을 것입니다 ... –
@tobias_k 네, 맞습니다. 사실 나는 실수로 게시물을 작성했지만 지금은 게시물을 업데이트했습니다. – Patrick