Error when using extract method in python not , "nothing to repeat at position 1"

61 Views Asked by At

I am a beginner to python and I am trying to search a certain string and extract 120 characters of text following the searched string from an excel cell in a table. I am trying to use the extract method in Python.

df\[column_label\] = df\[column_label\].str.extract(pattern, expand=False)

on running the code I get an error "re.error: nothing to repeat at position 1"

================================================================= `The code I am trying to run.

Read Excel file

\`\`
file_path = 'myexcel.xlsx'
sheet_name = 'spreadsheet'
df = pd.read_excel(file_path, sheet_name='spreadsheet')

df['data'] = df['Comments and Work notes']  
search_string1 = "xxxxxxxxxxxxxxxxxxxx"
column_label = 'Comments and Work notes'
pattern = fr'({search_string1},{{120}})'
print(pattern)
df[column_label] = df[column_label].str.extract(pattern, expand=False)
matched_pattern = df[column_label].str.extract(pattern, expand=False)
print(matched_pattern)

```

=================================================================

From a excel table (column) I need to search a string and extract 120 char that follows the searched string and copy it to the next column.

I tried the re.escape method on python. the method overcomes the error but it does return any value and the output is none


\`\`
\`df\['data'\] = df\['Comments and Work notes'\]  
search_string1 = "xxxxxxxxxxxxxxxxxxxx"
escaped_search_string1 = re.escape(search_string1)
column_label = 'Comments and Work notes'pattern = fr'({escaped_search_string1},{{120}})'
print(pattern)
df\[column_label\] = df\[column_label\].str.extract(pattern, expand=False)
matched_pattern = df\[column_label\].str.extract(pattern, expand=False)
print(matched_pattern)\`

0

There are 0 best solutions below