我已经在 Python 中导入了 csv 文件。我正在尝试删除该文件中括号内的文本。
我写了以下代码:
import csv
with open('example.csv', 'r') as csv_file:
csv_reader = csv.reader(csv_file)
#for line in csv_reader:
#print(line)
def a(test_str):
ret = ''
skip1c = 0
skip2c = 0
for i in test_str:
if i == '[':
skip1c += 1
elif i == '(':
skip2c += 1
elif i == ']' and skip1c > 0:
skip1c -= 1
elif i == ')' and skip2c > 0:
skip2c -= 1
elif skip1c == 0 and skip2c == 0:
ret += i
return ret
x = csv_reader
x = a(x)
for line in csv_reader:
print(line)
以下是我的输出中的几行:
['CompanyA (CA)']
['CompanyB']
['CompanyC']
['CompanyD (CD)']
['CompanyE (CE)']
如您所见,括号中的文本仍然存在。该代码似乎不适用于该文件。
我想删除括号之间的整个单词。例如,“ CompanyA(CA)”的输出应仅为 CompanyA ,而不是 CompanyA CA 。
下面是上面代码部分的示例,确实提供了预期的结果。
def a(test_str):
ret = ''
skip1c = 0
skip2c = 0
for i in test_str:
if i == '[':
skip1c += 1
elif i == '(':
skip2c += 1
elif i == ']' and skip1c > 0:
skip1c -= 1
elif i == ')'and skip2c > 0:
skip2c -= 1
elif skip1c == 0 and skip2c == 0:
ret += i
return ret
x = "ewq[a [(b] ([c))]] This is a sentence. (once a day) [twice a day]"
x = a(x)
print x
print repr(x)
这是该代码的输出:
ewq This is a sentence.
任何帮助将不胜感激。 谢谢您的时间:)
答案 0 :(得分:4)
您可以使用正则表达式来实现。下面的代码应该可以工作。
import re
input_text = 'ewq[a [(b] ([c))]] This is a sentence. (once a day) [twice a day]'
print re.sub(r'[\(\[]+.*?[\)\]]+', '', input_text)
以上代码适用于任何输入。请在下面的代码中找到第一个块中的输入内容。
csv_reader = ['CompanyA (CA)', 'CompanyC', 'CompanyD (CD)']
for line in csv_reader:
print re.sub(r'[\(\[]+.*?[\)\]]+', '', line)
答案 1 :(得分:1)
您还可以使用内置的find
和rfind
以及切片。从开头到开头的括号,再加上第一个开头的括号之后的第一个结尾的括号中的字符串。
def a(ts):
while '[' in ts:
ts=ts[:ts.rfind('[')]+ts[ts.find(']',ts.rfind('['))+1:]
while '(' in ts:
ts=ts[:ts.rfind('(')]+ts[ts.find(')',ts.rfind('('))+1:]
return ts
x = "ewq[a [(b] ([c))]] This is a sentence. (once a day) [twice a day]"
x = a(x)
print x