我在文本文件中有大量的字符串,我想按如下方式在每个字符串周围放置倒排引号。把倒排引号围绕使用索引的字符串,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