简体   繁体   English

Python-排除双引号和括号内的数据

[英]Python - exclude data with double quotes and parenthesis

I have a list which contains set of words in single quotes and double quotes, now i want to grep all the data only in single quotes. 我有一个列表,其中包含一组用单引号和双引号引起的单词,现在我想仅将所有数据用单引号引起来。

Input_
sentence = ['(telco_name_list.event_date','reference.date)',"'testwebsite.com'",'data_flow.code',"'/long/page/data.jsp'"]

Output: 输出:

telco_name_list.event_date,reference.date,data_flow.code

I want to exclude the parenthesis and string in double quotes. 我想在双引号中排除括号和字符串。

Since you tagged the post as python-3.x , I'm assuming you want to write python code. 由于您将帖子标记为python-3.x ,因此我假设您要编写python代码。 Based on your example this would work: 根据您的示例,这可以工作:

#!/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()

This code iterates through the list, ignoring elements which begin with a single quote and filtering out parentheses from elements before printing them on stdout. 此代码遍历列表,忽略以单引号开头的元素,并在将元素打印到stdout之前过滤掉括号。 This code works for the given example, but may or may not be what you need as the exact semantics of what you want out are somewhat ambiguous (ie is it fine to filter out all parentheses or just the ones at the beginning and end of your elements?). 该代码适用于给定的示例,但可能不是您所需要的,因为您想要的确切语义有些含糊(即,可以过滤掉所有括号,也可以只过滤括号的开头和结尾处的括号就可以了)元素?)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM