去除字符串中的特殊字符(如换行符,制表符,空格等)
将日期字符串转换为日期对象
将数字字符串转换为数字类型(如整数,浮点数等)
分割字符串,并将其转换为列表或数组
将空字符串转换为NULL值
将字符串转换为大写或小写
将字符串中的千位分隔符去除
替换字符串中的某些字符或字符串
将字符串转换为布尔值
将字符串中的多个空格合并为单个空格
去除字符串中的特殊字符
import re
def remove_special_characters(string):
return re.sub(r'[^a-zA-Z0-9 \n\.]', '', string)
将日期字符串转换为日期对象
from datetime import datetime
def string_to_date(string, date_format):
return datetime.strptime(string, date_format)
将数字字符串转换为数字类型
def string_to_int(string):
return int(string)
def string_to_float(string):
return float(string)
分割字符串并转换为列表
def string_to_list(string, separator):
return string.split(separator)
将空字符串转换为NULL值
def empty_string_to_null(string):
if string.strip() == '':
return None
return string
将字符串转换为大写或小写
def to_upper_case(string):
return string.upper()
def to_lower_case(string):
return string.lower()
将字符串中的千位分隔符去除
def remove_thousands_separator(string):
return string.replace(',', '')
替换字符串中的某些字符或字符串
def replace_substring(string, old, new):
return string.replace(old, new)
将字符串转换为布尔值
def string_to_boolean(string):
if string.lower() == 'true':
return True
elif string.lower() == 'false':
return False
else:
raise ValueError("Invalid boolean string")
将字符串中的多个空格合并为单个空格
def collapse_whitespace(string):
return ' '.join(string.split())