簡體   English   中英

Python-使用Python腳本使用Regex從文本文件中提取數據

[英]Python-To Extract Data from Text file using Regex using python script

我想從我的文本文件中提取公司名稱(Samsung India Electronics Pvt. Ltd.),該文件出現在公司名稱之后的下一行。 我已經通過我的代碼提取了一些數據,但我無法提取公司名稱,因為我是 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)

公司名稱: 地址:
三星印度電子列兵。 有限公司
Regd 辦公室:6th Floor, DLF Centre, Sansad Marg, New Delhi-110001

SAMSUNG INDIA ELECTRONICS PVT LTD, MEDCHAL MANDAL HYDERABAD

RANGA REDDY DISTRICT HYDERABAD TELANGANA 501401 電話:1234567 傳真號碼:分公司:S5S2 - [SIEL]HYDERABAD
訂單號:1403543436
貨幣:印度盧比
發票編號:36S2I0030874
發票日期:15.12.2018
產品編號: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]

輸出:

 Samsung India Electronics Pvt. Ltd.

暫無
暫無

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

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