Я пытаюсь удалить круглые скобки и все данные внутри с использованием Python 3.
Я рассмотрел несколько различных потоков, в том числе здесь:
Как удалить круглые скобки ивсе данные в пределах использования Pandas / Python?
После получения:
re.sub(r"\(.*\)|\s-\s.*", r"", str1)
для запуска без ошибок, он не удалил содержимое из строки str1.
Затем я попробовал этот подход:
Как удалить текст в скобках из строки Python?
, чтобы удалить скобки и содержимое из файла перед его чтением ви сохранение в str1 - но я получаю эту ошибку:
Traceback (most recent call last):
File "sum_all.py", line 27, in <module>
data.append(line.replace(match.group(),'').strip())
AttributeError: 'NoneType' object has no attribute 'group'
Вот код, я, очевидно, новичок в этом и ценю любую помощь !!
# Python3 program to calculate sum of
# all numbers present in a str1ing
# containing alphanumeric characters
# Function to calculate sum of all
# numbers present in a str1ing
# containing alphanumeric characters
import re
import math
import pyperclip
import pandas
def find_sum(str1):
# Regular Expression that matches digits in between a string
return sum(map(int,re.findall('\d+',str1)))
def find_sum2(str2):
# Regular Expression that matches digits where hr follows short for hours
return sum(map(int,re.findall('(\d+)hr',str1)))
str2=0
# Regular Expression
data=[]
pattern=r'\(.+\)|\s\-.+'
with open('project.txt','r') as f:
for line in f:
match=re.search(pattern,line)
data.append(line.replace(match.group(),'').strip())
print(data)
# input alphanumeric str1ing
with open ("project.txt", "r") as myfile:
str1=myfile.read().replace('\n', '')
# Regular Expression that removes (*) and Normalizes White Space - didn't work
#re.sub(r"\(.*\)|\s-\s.*", r"", str1)
# Regular Expression that removes (*) - didn't work
#re.sub(r"\(.*\)", r"", str1)