在Python中,拆分汉字可以通过以下几种方法实现:
1. 使用Unicode编码范围进行筛选:
```python
import re
str1 = "我%$是,《速$@.度\发》中 /国、人"
res1 = "".join(re.findall(r'[\u4e00-\u9fa5]', str1))
print(res1) 输出:'我是速度发中国人'
2. 使用正则表达式去除所有非字母、数字、中文字符:
```python
str2 = "齐天大圣孙悟空六学家Zhang第1张jpg"
res2 = re.sub(r'[^a-zA-Z0-9\u4e00-\u9fa5]', '', str2)
print(res2) 输出:'齐天大圣孙悟空六学家Zhang第1张jpg'
3. 使用第三方库进行更复杂的文本处理,例如使用`pandas`读取Excel文件并拆分特定列中的字符:
```python
import pandas as pd
def extract_characters(file_path, sheet_name, column_name):
df = pd.read_excel(file_path, sheet_name=sheet_name)
df['中文'] = ''
df['其他字符'] = ''
for index, row in df.iterrows():
text = str(row[column_name])
chinese = ''
other = ''
for char in text:
if '\u4e00' <= char <= '\u9fff':
chinese += char
else:
other += char
df.at[index, '中文'] = chinese
df.at[index, '其他字符'] = other
return df
4. 将字符串转换为列表,并利用正则表达式去除标点符号:
```python
def splitChar(strObj, e=False):
if not e:
charList = list(strObj)
else:
strObj = re.sub(r'[^\w\s]', '', strObj)
charList = strObj.split(' ')
return charList
strObj1 = "笨鸟工具导航"
charList1 = splitChar(strObj1, False)
print(charList1) 输出:['笨鸟', '工具', '导航']
strObj2 = "hello, world!"
charList2 = splitChar(strObj2, True)
print(charList2) 输出:['hello', 'world']
以上方法可以帮助你在Python中拆分汉字。请根据你的具体需求选择合适的方法