如何实现将字符串中的起始单引号替换为{、结束单引号替换为}?
嘿,这就帮你搞定格式转换的需求!
转换后的目标内容
[ { 'Architecture & Planning' } { 'Banking' } { 'Financial Services' } { 'Financial Services' } { 'Internet' } { 'Hospitality' } { 'Marketing and Advertising' } ]
手动操作步骤(适合单次处理)
如果只是手动改这一段的话,两步就能完成:
- 把每一个开头的单引号
'替换成{ ' - 把每一个结尾的单引号
'替换成' },同时删掉原数组里的逗号分隔符就行
代码自动转换方案(适合批量处理)
要是你需要处理大量这类数组,可以用Python脚本一键转换:
original = ['Architecture & Planning', 'Banking', 'Financial Services', 'Financial Services', 'Internet', 'Hospitality', 'Marketing and Advertising'] # 把每个元素包装成 { 'xxx' } 的格式 formatted_elements = [f"{{ '{item}' }}" for item in original] # 拼接成最终的字符串格式 final_result = f"[ {' '.join(formatted_elements)} ]" print(final_result)
运行这段代码就能直接得到你想要的输出格式咯~
内容的提问来源于stack exchange,提问作者poonam
相关产品推荐
相关产品推荐

