私はすでに開発したアルゴリズムを最適化する方法を探しています。私の質問のタイトルによれば、コンマで区切られた文字列を処理しています。この文字列には、いくつかの埋め込みコンマが含まれることがあります。これは大きなデータのコンテキストですべて実行されるため、速度が重要です。私がここに持っているものは、私が必要とするすべてのことをしますが、私はそれを行うより速い方法があると信じなければなりません。あなたは何か提案があれば、私はそれらを聞くのが大好きです。前もって感謝します。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 module](https://docs.python.org/3/library/csv.html#csv.reader)をクリックします。文字列ストリームを読み込むときは、必ず 'quotechar'パラメータを使用してください。 – Abdou
はい、私は車輪を再発明するのが好きです。それは、私が想定していることがどのように機能するかを本当に理解する良い方法です。プラス、それは価値がある(あまりない)ために、私は自分のやり方がクールだと思う。 –