Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 当数据为特定格式时,尝试从excel解析数据_Python_Regex - Fatal编程技术网

Python 当数据为特定格式时,尝试从excel解析数据

Python 当数据为特定格式时,尝试从excel解析数据,python,regex,Python,Regex,我对regex很陌生。我尝试着把我试图抽出来的所有货币和数字数值在这些格式中的所有货币和数字数值在这些格式中的所有货币和数字数值在我试图抽出来的所有货币和数字数值在这些格式中的所有货币和数字在这些格式中的所有货币和数字数值在我尝试着拔出来的所有货币和数字在这些格式中的所有的货币和数字在我试图拔出来的所有货币和数字在这些格式中的所有的货币和数字都是($ \\\\\3535353535#\##\###35;;##\35;;\35;35;;;;\35;;35;\#;;\35;)。我不断得到下面的回溯错

我对regex很陌生。我尝试着把我试图抽出来的所有货币和数字数值在这些格式中的所有货币和数字数值在这些格式中的所有货币和数字数值在我试图抽出来的所有货币和数字数值在这些格式中的所有货币和数字在这些格式中的所有货币和数字数值在我尝试着拔出来的所有货币和数字在这些格式中的所有的货币和数字在我试图拔出来的所有货币和数字在这些格式中的所有的货币和数字都是($ \\\\\3535353535#\##\###35;;##\35;;\35;35;;;;\35;;35;\#;;\35;)。我不断得到下面的回溯错误

import re
import pandas as pd
df1= pd.read_excel(r'C:\Users\user.name\Desktop\py_extractnumbers.xlsx', sheet_name = 0)
rows = len(df1)
print(str('Begin loop'))
for i in range(rows):
    NoteValue = df1.loc[i]['Comments']
    compilecheck = re.compile(r'\d\d\d\d'|r'\d,\d\d\d'|r'$\d,\d\d\d'|r'$\d\d,\d\d\d'|r'$\d\d\d\d')
    test= compilecheck.search(str(NoteValue))
    if test != None:
        result = str(test)
        df1['Amount']= df1['Amount'].astype(str)
        df1.at[i,'Amount']=result
        df1.at[i,'Amount']  
        print(str(result))
    else:
        pass
TypeError: unsupported operand type(s) for |: 'str' and 'str'
回溯错误

import re
import pandas as pd
df1= pd.read_excel(r'C:\Users\user.name\Desktop\py_extractnumbers.xlsx', sheet_name = 0)
rows = len(df1)
print(str('Begin loop'))
for i in range(rows):
    NoteValue = df1.loc[i]['Comments']
    compilecheck = re.compile(r'\d\d\d\d'|r'\d,\d\d\d'|r'$\d,\d\d\d'|r'$\d\d,\d\d\d'|r'$\d\d\d\d')
    test= compilecheck.search(str(NoteValue))
    if test != None:
        result = str(test)
        df1['Amount']= df1['Amount'].astype(str)
        df1.at[i,'Amount']=result
        df1.at[i,'Amount']  
        print(str(result))
    else:
        pass
TypeError: unsupported operand type(s) for |: 'str' and 'str'

首先,使用一个正则表达式并使用量词:
\$?\d、\d{3}\$\d{2}、\d{3}
-
$
是正则表达式中的一个特殊字符,用于匹配行尾-因此,将其转义为字面美元符号
$
。您不能输入
re.compile()
这样的多个字符串。另外,请注意,
$
是regex(字符串结尾/行锚点)中的一个特殊字符。如果要匹配字面美元符号,则需要对其进行转义。如果有类似于
11111
的字符串,则可能还需要锚定正则表达式,以便不匹配
1111
(根据您的规则,它不应与此匹配)。类似于
(?。也许您最好让我们知道您试图与正则表达式匹配的规则,并给我们一些示例,说明哪些规则需要匹配,哪些规则不需要匹配?基于您的更新:
\d{5}\$\d、\d{3}\$\d{2}、\d{3}
-但是,您应该锚定它。您真的只想匹配4位和5位数字吗?