Tuesday, February 14, 2023

Control char

 import pandas as pd

import re


# create a sample dataframe

df = pd.DataFrame({'text': ['hello\tworld', 'hi\nthere', 'how\rare you?']})


# define a function to search for control characters and get their position

def find_control_char(string):

    # search for control characters using regular expression

    pattern = re.compile(r'[\x00-\x1F\x7F]')

    match = pattern.search(string)

    if match:

        return match.start()

    else:

        return None


# apply the function to the dataframe

df['control_char_pos'] = df['text'].apply(find_control_char)


print(df)