簡體   English   中英

為什么 python 正則表達式不匹配特殊字符?

[英]Why is python regex not matching special characters?

我想知道為什么以下不起作用。 該表達式適用於 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表示比賽開始

我希望這會有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM