python - 为什么 python 正则表达式不匹配特殊字符?
问题描述
我想知道为什么以下不起作用。该表达式适用于 Regex101.com。但是,当我将 ’ 添加到电子表格中时,它会返回一个空数组,而不是至少匹配该字符串。
这是正则表达式:
[^A-z0-9\s,.][^-_+=]
这就是我正在寻找的:
’
Â
在这里试试(它对我有用): https ://regex101.com/
这是代码:
import pandas as pd
import chardet
import csv
import re
def get_file_encoding(file):
rawdata = open(file, "rb").read()
encoding = chardet.detect(rawdata)['encoding']
return encoding
#Type in sanitized_ACAS_FULL_1
data = 'sanitized_ACAS_FULL_1.csv'
my_encoding = get_file_encoding(data)
#print(my_encoding)
my_encoding = 'UTF-8-SIG'
df = pd.read_csv(data, encoding=my_encoding, header=None, low_memory=False)
csv_rows = df.apply(lambda x: x.tolist(), axis=1)
sanitized_rows = []
for row in csv_rows:
for item in row:
index = row.index(item)
row[index] = str(item).strip()
if 'nan' in str(item).strip():
row[index] = "NA"
for row in csv_rows:
for item in row:
sanitized_rows.append(item)
match = []
for row in sanitized_rows:
for entry in row:
if re.match(r'[^A-z0-9\s,.][^-_+=]', entry):
match.append(entry)
print(match)
解决方案
(\GÂ)|(\Gâ)|(\G€)|(\G™)
这将分别获得您想要的字符。如果您希望将它们分组,您可以使用(\G’)
示例。记住这\G
意味着比赛的开始。
我希望这会有所帮助。
推荐阅读
- javascript - How to change alignment of popover from v-date-picker component?
- javascript - 带有流程的 Docker 需要钩子不起作用?
- java - 如何在第三个对象列表中映射两个字符串类型列表
- vb.net - Default inherited button text
- python - Django save file on localhost
- ruby-on-rails - Convert Active Record Object into Hash
- active-directory - Keycloak AD FS Interaction
- esp8266 - NodeMCU - Unable to change GPIO Output pins to turn off
- python - 如何修复“TypeError:'Series' 对象是可变的”?在 Python 中与数据框和字典有关
- javascript - 为数组中的多个但相同的对象附加唯一值