알려주세요. 학습 점으로 사용하겠습니다. 나는 초보자이다.느린 파이썬 파일 I : O; 루비는 이것보다 더 잘 실행됩니다. 잘못된 언어를 얻었습니까?
25mb 파일을 여러 개의 작은 파일로 분할하려고합니다.
친절한 전문가가 나에게 루비 줄을 줬다. 그것은 아름답게 빨리 작동합니다. 그래서, 나는 파이썬 스크립트로 그것을 모방 배우기 위해서. 이것은 3 발 고양이처럼 느리게 움직입니다. 왜 누군가가 저에게 말할 수 있는지 궁금합니다.
내 파이썬 스크립트
##split a file into smaller files
###########################################
def splitlines (file) :
fileNo=0001
outFile=open("C:\\Users\\dunner7\\Desktop\\Textomics\\Media\\LexisNexus\\ele\\newdocs\%s.txt" % fileNo, 'a') ## open file to append
fh = open(file, "r") ## open the file for reading
mylines = fh.readlines() ### read in lines
for line in mylines: ## for each line
if re.search("Copyright ", line): # if the line is equal to the regex
outFile.close() ## close the file
fileNo +=1 #and add one to the filename, starting to read lines in again
else: # otherwise
outFile=open("C:\\Users\\dunner7\\Desktop\\Textomics\\Media\\LexisNexus\\ele\\newdocs\%s.txt" % fileNo, 'a') ## open file to append
outFile.write(line) ## then append it to the open outFile
fh.close()
구루의 루비 1.9 스크립트
g=0001
f=File.open(g.to_s + ".txt","w")
open("corpus1.txt").each do |line|
if line[/\d+ of \d+ DOCUMENTS/]
f.close
f=File.open(g.to_s + ".txt","w")
g+=1
end
f.print line
end
속도와 관련된 번호를 게시 할 수 있습니까? –
스크립트는 다른 작업을 수행합니다. 파이썬 스크립트는 작성하는 거의 모든 행에 대해 새 파일을 엽니 다. 그것은 확실히 느리지 만, 당신은 무엇을 기대합니까? –
또한 Ruby 스크립트는 출력을 위해 여러 파일을 엽니 다. –