嗨:)我無法弄清楚程序中的錯誤是否可以請你幫我解決。謝謝.. :)python - 語法錯誤
輸入文件包含以下內容:
3. भारत का इतिहास काफी समृद्ध एवं विस्तृत है।
57. जैसे आज के झारखंड प्रदेश से, उन दिनों, बहुत से लोग चाय बागानों में मजदूरी करने के उद्देश्य से असम आए।
(它基本上樣品,我需要得到在印地文附加到每個字的輸出字位置的句子)
EG的輸出的第一句話是這樣的:
3. भारत(1) का(2) इतिहास(3) काफी(4) समृद्ध(5) एवं(6) विस्तृत(7) है(8) ।(9)
我應該得到類似的運算爲下面的句子(S)
的代碼看起來是這樣的:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# encoding: utf-8
separators = [u'।', ',', '.']
text = open("hinstest1.txt").read()
#This converts the encoded text to an internal unicode object, where
# all characters are properly recognized as an entity:
text = text.decode("UTF-8")
#this breaks the text on the white spaces, yielding a list of words:
words = text.split()
counter = 1
output = ""
#if the last char is a separator, and is joined to the word:
for word in words:
if word[-1] in separators and len(word) > 1:
#word up to the second to last char:
output += word[:-1] + u'(%d) ' % counter
counter += 1
#last char
output += word[-1] + u'(%d) ' % counter
else:
output += word + u'(%d) ' % counter
counter += 1
print output
我得到的錯誤是:
File "pyth_hinwp.py", line 22
output += word[-1] + u'(%d) ' % counter
^
SyntaxError: invalid syntax
我知道這個問題是類似於我剛纔問的東西,但因爲我不是能夠成功地執行一些給我早些答案,因此我有點重構問題到我目前陷入困境的地方。
無法在Python 2.5.2上重現此錯誤! – batbrat 2010-02-20 06:17:34