我在文本文件中有大量的字符串,我想按如下方式在每個字符串周圍放置倒排引號。把倒排引號圍繞使用索引的字符串,python
文本文件包含這麼多的線路,如:
{created_at:2014年7月7日,文章:土耳其政府已經 繪製的路線圖取締庫爾德工人黨武裝的回報,誰 爲了在土耳其東南部開拓出一個 獨立的國家採取了對土耳其國家武器。}
,我想插入倒報價周圍的日期和文章內容是這樣的...
{created_at:「2014年7月07,」文章:「土耳其政府已經 繪製的取締庫爾德工人黨的武裝分子返回的路線圖誰 爲了開拓拿起武器反對土耳其政府使用蟒蛇指數法在土耳其東南部「}一個 獨立的狀態..
但我得到的結果作爲{created_at : "July 07", 2014, article : "The Turkish government has drawn a roadmap for the return of militants of the banned PKK, who took up arms against the Turkish state in order to carve out a separate state in southeastern Turkey}
..因此它被放置引號錯了位置。
這裏是我的代碼:此粗葉文件的讀/寫你
f = open("textfile.txt", "r")
for item in f:
first_comma_pos = item.find(",")
print first_comma_pos
first_colon_pos = item.find(" : ")
print first_colon_pos
second_comma_pos = item.find(",", first_comma_pos)
second_colon_pos = item.find(" : ", second_comma_pos)
print second_colon_pos
item = (item[:first_colon_pos+3] +
'"' + item[first_colon_pos+3:second_comma_pos] + '"' +
item[second_comma_pos:second_colon_pos+3] +
'"' + item[second_colon_pos+3:-1] + '"\n')
print item
saveFile= open("result.txt", "a")
saveFile.write(item)
saveFile.write('\n')
saveFile.close()
......,問題是......? – 2015-01-26 19:22:01
你的問題有兩個問題:1)你沒有說明問題是什麼,2)這可能是一個[XY問題](http://meta.stackexchange.com/questions/66377/what-is-the -xy-problem)的問題。 – Roberto 2015-01-26 19:22:33
更新了這個問題,我沒有得到任何錯誤,但是我的代碼將倒置的引號放在錯誤的位置,如問題中所示。 – 2015-01-26 19:26:56