[英]How to replace a character at beginning and end of a string in python
我有一個字符串:
path = "{'ResponseMetadata': {'Flag': 'Processed', 'Message': 'File Ingested successfully', 'INGEST_PATH': ['inward/emr_batch/manual_cars/xy99', ""manual_cars/xy99/2020/08/12/145938/ABC KPI's Jan 2018.csv""]}}
我只想在字符串的開頭和結尾替換 ' 而不是在字符串的中間。
我正在嘗試使用替換 python 的 function 來執行此任務
path = path.replace("\'", "\"")
但上面的方法是替換字符串中的“每一個地方”,得到的 output 是
{"ResponseMetadata": {"Flag": "Processed", "Message": "File Ingested successfully", "INGEST_PATH": ["inward/emr_batch/manual_cars/xy99", ""manual_cars/xy99/2020/08/12/145938/ABC KPI"s Jan 2018.csv""]}}
但我不希望 KPI 更改為 KPI"s,我可以實現此目的的一種方法是使用如下所示再次替換 function 以獲得所需的 output ,但是下面的問題是我需要繼續添加替換為每個新場景,我希望有通用解決方案可以在這里提供幫助。
path = path.replace("\'", "\"").replace('"s', "'s")
問候馬希
首先,您的字符串無效。 所以我用三引號來定義它:
path = """{'ResponseMetadata': {'Flag': 'Processed', 'Message': 'File Ingested successfully', 'INGEST_PATH': ['inward/emr_batch/manual_cars/xy99', ""manual_cars/xy99/2020/08/12/145938/ABC KPI's Jan 2018.csv""]}}"""
如何迭代字符串的字符並檢查isalpha()
周圍的字符:
out = []
for ix, char in enumerate(path):
# first make sure we have a surrounding character on each side
if ix > 0 and ix < len(path):
# now check for a quote and make sure that one of the surrounding characters are not letters
if char == "'" and not (path[ix-1].isalpha() and path[ix+1].isalpha()):
# if we find that case, replace the single quote
char = '"'
# add the character to the out list
out.append(char)
# join all the characters back together
output = ''.join(out)
print(output)
結果(同樣,直接粘貼是無效的,除非是三引號):
{"ResponseMetadata": {"Flag": "Processed", "Message": "File Ingested successfully", "INGEST_PATH": ["inward/emr_batch/manual_cars/xy99", ""manual_cars/xy99/2020/08/12/145938/ABC KPI's Jan 2018.csv""]}}
這是一個簡單的嘗試,您可以簡單地替換所有內容,然后替換雙引號后跟字母 s,如下所示
import re
path = '''
{'ResponseMetadata': {'Flag': 'Processed', 'Message': 'File Ingested successfully', 'INGEST_PATH': ['inward/emr_batch/manual_cars/xy99', ""manual_cars/xy99/2020/08/12/145938/ABC KPI's Jan 2018.csv""]}}
'''
path_ = re.sub('\'', '\"', path)
print(re.sub('\"s','\'s', path_))
output
{"ResponseMetadata": {"Flag": "Processed", "Message": "File Ingested successfully", "INGEST_PATH": ["inward/emr_batch/manual_cars/xy99", ""manual_cars/xy99/2020/08/12/145938/ABC KPI's Jan 2018.csv""]}}
你可以定位到任何你想替換的地方,比如
print(re.sub('KPI\"','KPI\'', path_))
我會推薦正則表達式。
https://en.m.wikibooks.org/wiki/Python_Programming/Regular_Expression
https://docs.python.org/3/library/re.html
第二個鏈接顯示正則表達式語言。
import re
path = """{'ResponseMetadata': {'Flag': 'Processed', 'Message': 'File Ingested successfully', 'INGEST_PATH': ['inward/emr_batch/manual_cars/xy99', ""manual_cars/xy99/2020/08/12/145938/ABC KPI's Jan 2018.csv""]}}"""
m=re.sub(r"'([^']*)'",r'"\1"',path)
print(m)
sub 具有以下簽名: re.sub(pattern, repl, string, count=0, flags=0)
結果: {“ResponseMetadata”:{“Flag”:“已處理”,“消息”:“文件攝取成功”,“INGEST_PATH”:[“inward/emr_batch/manual_cars/xy99”,“”manual_cars/xy99/2020/08 /12/145938/ABC KPI 的 Jan 2018.csv""]}}
此致
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.