我有一个列表,其中包含一组用单引号和双引号引起的单词,现在我想仅将所有数据用单引号引起来。
Input_
sentence = ['(telco_name_list.event_date','reference.date)',"'testwebsite.com'",'data_flow.code',"'/long/page/data.jsp'"]
输出:
telco_name_list.event_date,reference.date,data_flow.code
我想在双引号中排除括号和字符串。
由于您将帖子标记为python-3.x
,所以我假设您要编写python代码。根据您的示例,这可以工作:
#!/usr/bin/env python3
Input_sentence = ['(telco_name_list.event_date','reference.date)',"'testwebsite.com'",'data_flow.code',"'/long/page/data.jsp'"]
comma = False
result = []
for i in range(len(Input_sentence)):
e = Input_sentence[i]
if e.startswith("'"): continue
e = e.replace('(', '').replace(')', '')
if comma:
print(",", end="")
comma = True
print(e, end="")
print()
此代码遍历列表,忽略以单引号开头的元素,并在将元素打印到stdout之前过滤掉括号。该代码适用于给定的示例,但可能不是您所需要的,因为您想要的确切语义有些含糊(即,可以过滤掉所有括号,也可以只过滤括号的开头和结尾处的括号就可以了)元素?)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句