Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.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使用正则表达式和Python脚本从文本文件中提取数据_Python_Regex - Fatal编程技术网

Python使用正则表达式和Python脚本从文本文件中提取数据

Python使用正则表达式和Python脚本从文本文件中提取数据,python,regex,Python,Regex,我想从我的文本文件中提取公司名称(三星印度电子私人有限公司),该文件位于公司名称后的下一行。我已经用代码提取了一些数据,但我无法提取公司名称,因为我是python或python正则表达式的新手 import re hand = open(r'C:\Users\sachin.s\Downloads\wordFile_Billing_PrintDocument_7528cc93-3644-4e38-a7b3-10f721fa2049.txt') copy=False for line in hand

我想从我的文本文件中提取公司名称(三星印度电子私人有限公司),该文件位于公司名称后的下一行。我已经用代码提取了一些数据,但我无法提取公司名称,因为我是python或python正则表达式的新手

import re
hand = open(r'C:\Users\sachin.s\Downloads\wordFile_Billing_PrintDocument_7528cc93-3644-4e38-a7b3-10f721fa2049.txt')
copy=False
for line in hand:
    line = line.rstrip()
    if re.search('Order Number\S*: [0-9.]+', line):
        print(line)
    if re.search('Invoice No\S*: [0-9.]+', line):
        print(line)
    if re.search('Invoice Date\S*: [0-9.]+', line):
        print(line)
    if re.search('PO No\S*: [0-9.]+', line):
        print(line)
公司名称:地址:
三星印度电子私人有限公司
注册办公室:新德里Sansad Marg DLF中心6楼-110001

三星印度电子私人有限公司,MEDCHAL MANDAL HYDERABAD

海得拉巴特兰加纳兰加雷迪区501401 电话:1234567 传真号码: 分行:S5S2-[SIEL]海得拉巴
订单号:1403543436
货币:印度卢比
发票编号:36S2I0030874
发票日期:2018年12月15日
PI编号:5929947652使用正则表达式:

import re

data = """
Firm Name: Address:
Samsung India Electronics Pvt. Ltd.
Regd Office: 6th Floor, DLF Centre, Sansad Marg, New Delhi-110001

SAMSUNG INDIA ELECTRONICS PVT LTD, MEDCHAL MANDAL HYDERABAD

RANGA REDDY DISTRICT HYDERABAD TELANGANA 501401 Phone: 1234567 Fax No: Branch: S5S2 - [SIEL]HYDERABAD
Order Number: 1403543436
Currency: INR
Invoice No: 36S2I0030874
Invoice Date: 15.12.2018
PI No: 5929947652
"""

result = re.findall('Address:(.*)Regd', data, re.MULTILINE|re.DOTALL)[0]
输出:


张贴几行玩具
txt
data您能提供您的文件内容吗?公司名称:地址:三星印度电子私人有限公司注册办事处:新德里Sansad Marg DLF中心6楼-110001@sachinsharma在您发布的文本前后再添加几行。在公司名称:地址后添加此问题:下一个文本在新行upto LTD.中。之后,数据再次在新行中,我只想提取(三星印度电子私人有限公司)hi@zaraki,但此正则表达式仅适用于此文件。我有许多文本文件公司名称:地址:相同,但可以代替Regd任何文本,则此regex为Faily我的文本格式公司名称:地址:新线三星印度电子私人有限公司新线Regd办公室:Sansad Marg DLF中心6楼,新德里-110001'@sachinsharma然后发布更多文本示例请提供您的电子邮件我将发送文本文件我更新文本文件格式请查看格式并感谢您抽出时间
 Samsung India Electronics Pvt. Ltd.