0
이미 개발 한 알고리즘을 최적화하는 방법을 찾고 있습니다. 내 질문의 제목을 말했듯이, 나는 쉼표로 구분 된 문자열을 처리하는 경우가 있는데, 그 중에는 쉼표가 포함될 수도 있습니다. 이것은 큰 데이터의 맥락에서 모두 이루어 지므로 속도가 중요합니다. 내가 여기있는 것은 내가 필요로하는 모든 일을하지만, 나는 그것을하는 더 빠른 방법이있을 것이라고 믿어야 만한다. 당신이 어떤 제안이라도 듣고 싶습니다. 미리 감사드립니다.Python을 사용하여 쉼표로 구분 된 문자열을 쉼표로 파싱
코드 :
import os,re
commaProblemA=re.compile('^"[\s\w\-()/*[email protected]!#%^\'&$\{\}|<>:0-9]+$')
commaProblemB=re.compile('^[\s\w\-()/*[email protected]!#%^\'&$\{\}|<>:0-9]*"$')
#example string
#these are read from a file in practice
z=',,"N/A","DWIGHT\'s BEET FARM,INC.","CAMUS,ALBERT",35.00,0.00,"NIETZSCHE,FRIEDRICH","God, I hope this works, fast.",,,35.00,,,"",,,,,,,,,,,"20,4,2,3,2,33","223,2,3,,34 00:00:00:000000",,,,,,,,,,,,0,,,,,,"ERW-400",,,,,,,,,,,,,,,1,,,,,,,"BLA",,"IGE6560",,,,'
testList=z.split(',')
for i in testList:
if re.match(commaProblemA,i):
startingIndex=testList.index(i)
endingIndex=testList.index(i)
count=0
while True:
endingIndex+=1
if re.match(commaProblemB,testList[endingIndex]):
diff=endingIndex-startingIndex
while count<diff:
testList[startingIndex]=(testList[startingIndex]+","+testList[startingIndex+1])
testList.pop(startingIndex+1)
count+=1
break
print(str(lineList))
print(len(lineList))
바퀴를 재 활성화 하시겠습니까? https://docs.python.org/2/library/csv.html – e4c5
[StringIO] (https://docs.python.org/3/library/io.html#io.StringIO)와 [ CSV 모듈] (https://docs.python.org/3/library/csv.html#csv.reader). 문자열 스트림을 읽을 때'quotechar' 매개 변수를 사용해야합니다. – Abdou
예, 저는 바퀴를 재발견하고 싶습니다. 그것은 내가 생각하는 것들이 어떻게 작용 하는지를 진정으로 이해하는 좋은 방법입니다. 또한, 가치가있는 것 (그다지)이 아니라면, 내 방식이 멋지다고 생각합니다. –