使用索引python在字符串周围加上倒引号
我在文本文件中有大量字符串,我想在每个字符串周围加上倒引号,如下所示 文本文件包含许多行,例如: {创建时间:2014年7月7日,文章:土耳其政府 为被取缔的库尔德工人党武装分子的回归绘制了路线图 拿起武器反对土耳其政府,以便开辟一条新的道路 土耳其东南部的独立州。} 我想在日期和文章内容周围插入倒引号,就像这样 {创建于:“2014年7月7日”,文章:“土耳其政府 为被取缔的库尔德工人党武装分子的回归绘制了路线图 拿起武器反对土耳其政府,以便开辟一条新的道路 使用python中的索引方法分离土耳其东南部的州“}” 但我得到的结果是{created_at:“July 07”,2014,文章:“土耳其政府已经为被取缔的库尔德工人党(PKK)武装分子的回归制定了路线图,他们拿起武器反对土耳其国家,以便在土耳其东南部建立一个独立的国家}。因此,他们将这些引述放在了错误的位置 这是我的密码:使用索引python在字符串周围加上倒引号,python,Python,我在文本文件中有大量字符串,我想在每个字符串周围加上倒引号,如下所示 文本文件包含许多行,例如: {创建时间:2014年7月7日,文章:土耳其政府 为被取缔的库尔德工人党武装分子的回归绘制了路线图 拿起武器反对土耳其政府,以便开辟一条新的道路 土耳其东南部的独立州。} 我想在日期和文章内容周围插入倒引号,就像这样 {创建于:“2014年7月7日”,文章:“土耳其政府 为被取缔的库尔德工人党武装分子的回归绘制了路线图 拿起武器反对土耳其政府,以便开辟一条新的道路 使用python中的索引方法分离土
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()
很黑但是
fix_json.py
import re,json
s = """{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.}"""
parts0 = s.split(":")
data = {}
for lhs,rhs in zip(parts0,parts0[1:]):
#: assume that the word directly preceding the ":" is the key
#: word defined by regex below
key = re.sub("[^a-zA-Z_]","",lhs.rsplit(",",1)[-1])
value = rhs.rsplit(",",1)[0]
data[key] = value
print json.dumps(data)
这一粗糙度将文件的读取/写入留给您…并根据您的示例对您的数据做出一些假设您非常准确,但有两个缺陷:-
- 因为没有添加额外的索引,所以您过去常常查找第一个逗号本身的位置
- 你的结尾
在你的“
之外。因此,它过去常常被扔到不合适的地方{
f = open("textfile.txt", "r")
for item in f:
first_comma_pos = item.find(",")
print item
print first_comma_pos
first_colon_pos = item.find(" : ")
print first_colon_pos
second_comma_pos = item.find(",", first_comma_pos+1) # Note change
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:-2] + '"}\n') # Note change
print item
saveFile= open("result.txt", "a")
saveFile.write(item)
saveFile.write('\n')
saveFile.close()
输出
{创建于:“2014年7月7日”,文章:“土耳其政府已为被取缔的库尔德工人党武装分子的回归绘制了路线图,他们拿起武器反对土耳其国家,以便在土耳其东南部建立一个独立的国家。”
如果数据始终是该格式,则可以从右侧逐位标记,例如:
s = """{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.}"""
created_at, a_sep, article_text = s.strip('{}').rpartition('article :')
start, c_sep, created_date = created_at.rpartition('created_at :')
new_string = '{{{} "{}", {} "{}"}}'.format(
c_sep,
created_date.strip(' ,'),
a_sep,
article_text.strip()
)
# {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."}
…问题是…?您的问题有两个问题:1)您没有说明问题是什么,2)这可能是一个问题。更新了问题后,我没有发现任何错误,但我的代码将倒引号放在了问题中指出的错误位置。我想您对字符串与repr…或者not@JoranBeasley我很确定这是个坏主意。垃圾输入,垃圾输出。这个解决方案让我很难过。我不反对……但鉴于提供的信息,我认为这就是OP想要的……真正正确的答案是调整其他脚本以输出有效数据(json或任何其他序列化数据)而不是自己编系列routine@JonKiparsky别难过,让你高兴起来!@JoranBeasley这是正确的-对这个问题的任何回答都是一个糟糕的答案。(但你的答案比另一个好得多…)帮他改正剧本做得好…可能比我的答案更具教育价值(+1)