[問題]從別的程式呼叫python出現不一樣的結果

看板Python作者 (復讎の業火)時間12年前 (2013/10/30 04:35), 編輯推噓2(207)
留言9則, 2人參與, 最新討論串1/1
各位版上的python先進大家好: 小弟最近因為研究需求開始接觸python,是個完全的新手, 由於小弟的研究領域是文件自動分類, 因此需要用到python裡極強大的nltk自然語言處理套件。 但是小弟的程式基礎都在C#上,也有已經打造好的文件分類系統, 估計短期內不可能整個用python重新打造出來, 因此最可能的方案還是將文字的處理部份交給python,再接到我以C#打造的系統做分類。 小弟知道這麼做有點逆天行事,但是要把我的作業流程給自動化似乎沒有更好的辦法, 因此就是利用python的.py檔案可以直接執行的特性,讓C#呼叫ProcessRun來執行它, (理論上)效果其實就像從cmd來執行一樣,這段在C#裡的程式碼長這樣: System.Diagnostics.Process ProcessRun = new Process(); ProcessRun.StartInfo.FileName = @"C:\Python27\python.exe"; ProcessRun.StartInfo.Arguments = Directory.GetCurrentDirectory() + @"\nltkFacade.py"; ProcessRun.StartInfo.WorkingDirectory = Directory.GetCurrentDirectory(); ProcessRun.StartInfo.UseShellExecute = false; ProcessRun.StartInfo.RedirectStandardInput = true; ProcessRun.StartInfo.RedirectStandardOutput = true; ProcessRun.StartInfo.CreateNoWindow = true; ProcessRun.Start(); Console.WriteLine("finish execute python script "); ProcessRun.WaitForExit(); Console.WriteLine(ProcessRun.StandardOutput.ReadToEnd()); ProcessRun.Close(); 然後被執行的nltkFacade.py長這個樣子: ========================================================= #-*- coding:big5 -*- import os #讀取程式目前路徑用 import sys #為了能夠吃進主程式外部輸進的參數 import nltk #引入自然語言處理模組 import io #檔案io from nltk.corpus import stopwords #去無用字 import re #需要用正則表示式移除標點與數字 from nltk.corpus import wordnet as wn #2013/10/26改用wordnet來做字根還原 #把下面三個文字處理函式串接在一起 def superTextHandler(inputString): #先移除無用符號及數字 inputString=removePuncuhationAndNumbers(inputString) #先轉換小寫與斷詞 tokens=lowerAndTokenlization(inputString) #去無用字 tokensRemoveStopWords= removeStopwords(tokens) #字根還原 return wordNetStemming(tokensRemoveStopWords) #這個去無用字函式是從網路上抄來的,確定能用 def removeStopwords(palabras): print ("remove stopwords activated!") return [ word for word in palabras if word not in stopwords.words("english") ] #移除標點與數字,只要排除非英文字母以外的所有字元就好 def removePuncuhationAndNumbers(input): return re.sub(r"[^A-Za-z\s]+"," ",input) #重要,符號的取代要用空白鍵,不然會沾粘 #word net的字根還原函式, 輸入已經斷詞的list def wordNetStemming(tokens): str_list = [] #製造一個類似C#裡StringBuilder的東西 for token in tokens: str_list.append(wordNetStemmingOneWord(token)+" ") #字根還原完了,記得要加那個空白鍵不然會沾粘 return "".join(str_list) #這就像最終的StringBuilder.ToString #使用wordnet做單個字詞的字根還原,只進行名詞與動詞,基本上它還是給人呼叫 def wordNetStemmingOneWord(input): #在python裡None應該就是null try: if(wn.morphy(input, wn.NOUN)!=None): input=wn.morphy(input, wn.NOUN) if(wn.morphy(input, wn.VERB)!=None): input=wn.morphy(input, wn.VERB) except Exception, e: print e return input #斷詞函式,輸入尚未斷詞之字串,同時完成轉換小寫 def lowerAndTokenlization(input): try: #不小心讀到空字串時會導致斷詞函式crash,所以用try包起來 return nltk.word_tokenize(input.lower()) #斷詞 except Exception: return nltk.word_tokenize("null") #斷詞 #處理單一的文字檔案,讀出來改好再寫回同一檔案 def dealOneFile(para): orgdoc=io.open(para, "r", encoding="UTF-8") modifiedDoc=" " try: modifiedDoc=orgdoc.read() except Exception, e: print e wholeParagraph=superTextHandler("".join(modifiedDoc)) orgdoc.close() mdfdoc=io.open(para, "w") mdfdoc.write(wholeParagraph.decode("UTF-8")) mdfdoc.close() print("perform text operation successfully! "+wholeParagraph) #主程式 if __name__ == '__main__': #針對我所需要的工作資料夾走訪每個檔案 for root, dirs, files in os.walk(os.getcwd()+"\\txtFiles\\allStandards\\"): print ("show folder: "+root) #輸出根目錄,debug for f in files: print ("show file" +os.path.join(root, f)) #輸出要改的檔案,debug dealOneFile(os.path.join(root, f)) #執行單一文字檔的修改 ======================================================================= 而目前小弟遭遇的情況是: 這個nltkFacade.py沒有問題,不論從windows上直接點兩下執行, 或是從主控台cmd裡輸入命令列來執行,都可以正確運作, 可是透過C#來呼叫時,它「沒有完整執行」, 這個程式是針對一個資料夾裡的所有文字檔案作修改, 從C#呼叫的結果是「只有其中一部份的檔案有順利被修改」,其他部份則都無動於衷。 然而,由於這個python檔案本身在cmd下已經能被正確執行, 在正確執行的狀態下當然不會回報任何錯誤與例外訊息, 但透過C#來呼叫時,C#的Console上並不轉達python所負責輸出的訊息, 也就是我追蹤不到任何Exception的原因,只好來到貴版求援, 懇請各位python高手不吝賜教,幫小弟看看可能的問題在哪,感激不盡! P.S.小弟還有嘗試過用p2exe將這個py檔案編譯成exe檔, 就小弟使用C#的經驗,呼叫exe檔的結果通常不令人意外, 但是執行exe檔的結果仍和執行這個py檔案一樣,就是只有一部份的文件被修改。 -- 灰心喪志的人,如洩了氣的皮球,一蹶不振; 消極頹唐的人,如洩了精的雞巴,無望再舉。 而,打個手槍沒有目標的痛苦,就是現實世界的殘酷。 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 130.126.241.12 ※ 編輯: CYBASTER 來自: 130.126.241.12 (10/30 04:41)

10/30 08:18, , 1F
把RedirectStandardOutput關掉,console就會有py output了
10/30 08:18, 1F

10/30 11:18, , 2F
10/30 11:18, 2F

10/30 11:18, , 3F
我以前用 Python For .Net 做過一些實驗,感覺不錯
10/30 11:18, 3F

10/30 11:19, , 4F
PS : 這不是 Iron Python
10/30 11:19, 4F

10/30 11:21, , 5F
而你的問題, 也可以查查看 Iron Python
10/30 11:21, 5F

10/30 11:22, , 6F
還有 C# 應該可以看到 Python 的 Standard Out, 印出來吧
10/30 11:22, 6F
謝謝timTan大大的建議,我一開始也在想用Iron Python來接, 但是因為我使用的nltk套件不支援Iron Python只好作罷。 ^^ ※ 編輯: CYBASTER 來自: 50.148.113.168 (10/30 12:48)

10/30 13:56, , 7F
看可以try 剛剛貼的 還有 python for .NET
10/30 13:56, 7F

10/30 13:56, , 8F
那根 iron python 是不一樣的東西
10/30 13:56, 8F

10/30 21:08, , 9F
所以看到exception是什麼了沒?
10/30 21:08, 9F
文章代碼(AID): #1IS1mTcN (Python)
文章代碼(AID): #1IS1mTcN (Python)