Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.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 字典和正则表达式匹配用于构建URL的对象_Python_Regex_Dictionary_Object_Match - Fatal编程技术网

Python 字典和正则表达式匹配用于构建URL的对象

Python 字典和正则表达式匹配用于构建URL的对象,python,regex,dictionary,object,match,Python,Regex,Dictionary,Object,Match,试图从这里的pdf数据中预填充表单,我一直在使用一些正则表达式匹配对象和字典 “缩写”代码: 跳过Py2PDF部分,因为这看起来很好。请原谅我的命名习惯 ### Open text file, search for field contents, and define them with open (pdffile+'-text.txt', 'r') as text_file: text = text_file.read() entering_email_value = reg

试图从这里的pdf数据中预填充表单,我一直在使用一些正则表达式匹配对象和字典

“缩写”代码:

跳过Py2PDF部分,因为这看起来很好。请原谅我的命名习惯

### Open text file, search for field contents, and define them
with open (pdffile+'-text.txt', 'r') as text_file:
    text = text_file.read() 
    entering_email_value = regex.search(r'(?<=Email:\|)(.*?)(?=\|)(?=.*\|Manager Information:)', text) or ["---"]
    uid_number_value = regex.search(r'(?<=UID Number:\|)(.*?)(?=\|)', text) or ["---"]
    fname_value = regex.search(r'(?<=First Name:\|)(.*?)(?=\|)', text) or ["---"]
    lname_value = regex.search(r'(?<=Last Name:\|)(.*?)(?=\|)', text) or ["---"]
    add_1_value = regex.search(r'(?<=Last Name:\|.*)(?<=Address:\|)(.*?)(?=\|)(?=.*Employee Information:)', text) or ["---"]
    city_value = regex.search(r'(?<=Last Name:\|.*)(?<=City & State:\|)(.*?)(?=,)(?=.*Employee Information:)', text) or ["---"]
    state_value = regex.search(r'(?<=Last Name:\|.*)(?<=, )(.*?)(?= )(?=.*Employee Information:)', text) or ["---"]
    zip_value = regex.search(r'(?<=Last Name:\|.*)(?<=[A-Z][A-Z] )(.*?)(?=\|)(?=.*Employee Information:)', text) or ["---"]

getVars = {entering_email: entering_email_value.group(), 
            uid_number: uid_number_value.group(),
            email: email_value.group(),
            fname: iw_fname_value.group(),
            lname: iw_lname_value.group(),
            city: city_value.group(),
            state: state_value.group(),
            zip: zip_value.group()
            }

webbrowser.open(url + urllib.parse.urlencode(getVars), new=0, autoraise=True)
###打开文本文件,搜索字段内容并定义它们
打开(Pdfile+'-text.txt',r')作为文本文件:
text=text\u file.read()

输入\u email\u value=regex.search(r'(?当然,这在之前已经得到了回答:


看起来这只是通过添加“|$”将“”默认值嵌入到我的语句中的问题

请尽可能清楚地解释你的问题。问一些具体的问题也会有帮助。你为什么要逃避?我觉得你的正则表达式很奇怪。我不确定它们是否正是你想要它们做的。例如,随机选择一个
(?=\ \ \ \)
的意思是,如果后跟this,您希望在此之前的文本匹配。而这将匹配的是一个“|”符号。很抱歉,我不清楚-我认为OR语句被忽略,因为当没有匹配时,它会抛出一个异常错误“'list'对象没有属性'group'”-我希望能够只分配“”或“”.我在逃避|因为在我知道使用DOTALL标志之前我用|替换了\n-所以它现在有点像一个分隔符。这里有一个巨大的学习曲线。
### Open text file, search for field contents, and define them
with open (pdffile+'-text.txt', 'r') as text_file:
    text = text_file.read() 
    entering_email_value = regex.search(r'(?<=Email:\|)(.*?)(?=\|)(?=.*\|Manager Information:)', text) or ["---"]
    uid_number_value = regex.search(r'(?<=UID Number:\|)(.*?)(?=\|)', text) or ["---"]
    fname_value = regex.search(r'(?<=First Name:\|)(.*?)(?=\|)', text) or ["---"]
    lname_value = regex.search(r'(?<=Last Name:\|)(.*?)(?=\|)', text) or ["---"]
    add_1_value = regex.search(r'(?<=Last Name:\|.*)(?<=Address:\|)(.*?)(?=\|)(?=.*Employee Information:)', text) or ["---"]
    city_value = regex.search(r'(?<=Last Name:\|.*)(?<=City & State:\|)(.*?)(?=,)(?=.*Employee Information:)', text) or ["---"]
    state_value = regex.search(r'(?<=Last Name:\|.*)(?<=, )(.*?)(?= )(?=.*Employee Information:)', text) or ["---"]
    zip_value = regex.search(r'(?<=Last Name:\|.*)(?<=[A-Z][A-Z] )(.*?)(?=\|)(?=.*Employee Information:)', text) or ["---"]

getVars = {entering_email: entering_email_value.group(), 
            uid_number: uid_number_value.group(),
            email: email_value.group(),
            fname: iw_fname_value.group(),
            lname: iw_lname_value.group(),
            city: city_value.group(),
            state: state_value.group(),
            zip: zip_value.group()
            }

webbrowser.open(url + urllib.parse.urlencode(getVars), new=0, autoraise=True)