Home > front end >  To find the next word of a particular word as often as possible from a string with python
To find the next word of a particular word as often as possible from a string with python

Time:07-06

I'm working with Python,I need to find next substring of all order-ids using python code.

str = """Delivered-To: [email protected]\r\nReceived: by 2002:a05:7022:2496:b0:41:f6c7:a91b with SMTP id cq22csp1730422dlb;\r\n        Sat, 2 Jul 2022 06:28:47 -0700 (PDT)\r\nX-Received: by 2002:a0d:e202:0:b0:317:a874:ed5e with SMTP id l2-20020a0de202000000b00317a874ed5emr23336541ywe.16.1656768526947;\r\n        Sat, 02 Jul 2022 06:28:46 -0700 (PDT)\r\nARC-Seal: i=1; a=rsa-sha256; t=1656768526; cv=none;\r\n        d=google.com; s=arc-20160816;\r\n        b=Bg13SC/ O6YTjTWpzhEous8ksXxmiAdmHMXf8vl0wayPdbAQxCOC3R5WLcYLs28Gr6\r\n         jljhCngfYqS/deGnQKupI7TJK0OIOgsRTGhWv2TuwAtHUEikt3MOiaVU2haFG/cR2LOA\r\n         4ebPyL/J2hn6Odq9b hHRHbMDX8tE6fQ kYAxsXUVK8EYv1HrDvN EVmeN8atTWraf  \r\n         2fG/FXP o0HbkGXg2XiQs5PDsjA0E3PUunLnQGJ/XbRBCFiPGfCwHpX3o7Pa 8l2yDAf\r\n         456AGAOKaJVwgoof/oEkAk2cJVShHVrcfaAdTAOB2WoVPxVtmLU8j9fRcRHzwNee4pQX\r\n         V5FA==\r\nARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816;\r\n        h=to:subject:message-id:date:from:mime-version:dkim-signature;\r\n        bh=AzAZ1C7TlSF/OObMPEe0rLBlSdgV/qpbwZsWlcKzlnw=;\r\n        b=EFPr72 Ffth A lV853gas2VFYUAywbGOG2F/w1EbzpVN3HgQUZkK2Tc23muE3Y0FZ\r\n         4YkIGJ5wNlacqn1ZPJZu2esguCx7Xc8quyl586zP4PdtYLUn Uwdg7U3PqNskYvUtKA4\r\n         7rX8KfHQpLKgPvidvtUtv50WszQNzGjHStvnM3jc0/TzVznPHuGy5JM 30ygUhcT22Sn\r\n         Abus2VLuIPWZt6xDzz9fH RZxtRLZlNo3kLssX5uI37T5QqppKEamqk6WPW7frPM/55S\r\n         KtcfjI/z frS1C5K3CDHofVGrn9I6suNQM/gAnMSBjoF8wekHO76iD6n4RzrifbUFj6c\r\n         g0Ww==\r\nARC-Authentication-Results: i=1; mx.google.com;\r\n       dkim=pass header.i=@gmail  dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com\r\nReturn-Path: <[email protected]>\r\nReceived: from mail-sor-f41.google.com (mail-sor-f41.google.com. [209.85.220.41])\r\n        by mx.google.com with SMTPS id s127-20020a255e85000000b00663e89b60cbsor10801684ybb.111.2022.07.02.06.28.46\r\n        for <[email protected]>\r\n        (Google Transport Security);\r\n        Sat, 02 Jul 2022 06:28:46 -0700 (PDT)\r\nReceived-SPF: pass (google.com: domain of [email protected] designates 209.85.220.41 as permitted sender) client-ip=209.85.220.41;\r\nAuthentication-Results: mx.google.com;\r\n       dkim=pass [email protected] header.s=20210112 header.b=ZkQiOE9S;\r\n       spf=pass (google.com: domain of [email protected] designates 209.85.220.41 as permitted sender) [email protected];\r\n       dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com\r\nDKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r\n        d=gmail.com; s=20210112;\r\n        h=mime-version:from:date:message-id:subject:to;\r\n        bh=AzAZ1C7TlSF/OObMPEe0rLBlSdgV/qpbwZsWlcKzlnw=;\r\n        b=ZkQiOE9StPCLPV6aN0tQ/qR0jr3WXSiXrPxz6ES9l4mbG4SAX4kTcFavTWxVTQSpcM\r\n         1Q9 mKsfQc0hSiWT2kJ7GMGLzCA8AYLZGdR6YRmrQtBUzVJLA5cOmPAPEt9Bi/IhDby7\r\n         eLyF1LOEZczl4lJpWf2WmJh6788RcwKgcoacAKL4Ic99fs3Jo1AvdJON15q095zhx/UB\r\n         XnGGcUWmmGReFq43 1ZD9eucbYLq5wzU37u9uSZ7Ufs86ZYsYLs1q6L741VyyfoBF20p\r\n         QjBGMLSqCSgHTg7 jNUlT ILvzgRbbiRZCojKVN4jdReM7iF7yjGppwbY8iLhzOSWXQj\r\n         Jfew==\r\nX-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r\n        d=1e100.net; s=20210112;\r\n        h=x-gm-message-state:mime-version:from:date:message-id:subject:to;\r\n        bh=AzAZ1C7TlSF/OObMPEe0rLBlSdgV/qpbwZsWlcKzlnw=;\r\n        b=qragdi6nn8PakvfOlZDy8mZg5Y7k7cpU/zYTujRt GbVhquNRl8pBttSyDoWayX/nE\r\n         zo/zg708WYFcA0y JCtRiQbE1tP4eacdgRjrqJVP4aWD2ekhUNV34hFamGHdSLru/kkA\r\n         F8LkW/FGUbMI5afo MsUwOOaBG572 cOFRC60pi5sOzkYMNv8z6/dsULvcmTaJSvx7vQ\r\n         TsK17/ B4YbrxjE4ObQWs5sYKTjh3gS4LwAKGMk2iz3urOM/xdiElRW/Devx1hsqValm\r\n         iR1T9AmKAXONN/hnsca XPgN9AF0CvzBM16AgcL/sJxGBTXQydhl/ONjzWEHi/rAHiQP\r\n         3FGQ==\r\nX-Gm-Message-State: AJIora/wEq AYfVnN5ZkuMS1fy2SFUsnu2fEa6M/g8facCXKwFg75XFI\r\n\tX6kZgkwnI3sZUFr65hyulusVlqMfP8weHYTN dJ9zo3tXY8=\r\nX-Google-Smtp-Source: AGRyM1sMX8yXEHExf7g/zdvb3g7Jm2YwY7gFCr8f7bDU66Cg8Lvzuwnk07HWkrMNQ690BI3h1ZphPN4TeqZNi6EdkeM=\r\nX-Received: by 2002:a05:6902:11c8:b0:664:6d14:4832 with SMTP id\r\n n8-20020a05690211c800b006646d144832mr19950147ybu.624.1656768526592; Sat, 02\r\n Jul 2022 06:28:46 -0700 (PDT)\r\nMIME-Version: 1.0\r\nFrom: example2 <[email protected]>\r\nDate: Sat, 2 Jul 2022 18:58:35  0530\r\nMessage-ID: <CAJhM7UN 1sQEJ80wRbU [email protected]>\r\nSubject: Testing\r\nTo: \"[email protected]\" <[email protected]>\r\nContent-Type: multipart/alternative; boundary=\"00000000000080aed005e2d27b4b\"\r\n\r\n--00000000000080aed005e2d27b4b\r\nContent-Type: text/plain; charset=\"UTF-8\"\r\n\r\nTesting mail\r\n\r\n--00000000000080aed005e2d27b4b\r\nContent-Type: text/html; charset=\"UTF-8\"\r\n\r\n<div dir=\"auto\">Dear Seller,Kunal Bapurao Suryvanshi has returned 8 of your packages successfully. Details of the packages delivered: Order ID 403-2972125-1298169 Tracking number 513321566504 Product (s)  Women's Velvet Sequence Sleeveless Blouse (ITGFHD-832_Pink_38) (1) Return reason Rejected Order ID 171-9205635-8965901 Tracking number 513318895278 Product (s) Georgette Sharara Suit for Women | Diamond Work Short Kurta with Sharara Plazzo | Kurta Sharara Set with Dupatta (Wine) (1) Return reason C-Returns Secure Returns* Delivered in a tamper proof bag with bag ID sptpr018296434 Order ID 407-6084850-2481910 Tracking number 513315647983 Product (s) Women's Velvet Sleeveless Blouse (ITGFHD-832_Black_38 4" Margin) (1) Return reason C-Returns Order ID 405-0318978-1176378 Tracking number 513312989677 Product (s) Women's Georgette Straight Kurta Sharara Set With Dupatta (DFR-8_Yellow_2XL) (1) Return reason Rejected Order ID 403-7879336-4487536 Tracking number 513318026051 Women's Velvet Sleeveless Blouse (ER-49_Red_Free Size) (1) Return reason C-Returns Order ID 171-5938487-9085769 Tracking number 513553964773 Product (s) Readymade Salwar Suit for Women with Dupatta | Embroidered Neck Kurta and Sharara with Dupatta Set(XXL) Purple (1) Return reason Amazon CS Order ID 408-5127580-8983538 Tracking number 513318975508 Product (s) Women's Velvet heavy Sequence Readymade Sleeveless Blouse (38 Size   4inch Margin, red) (1) Return reason Rejected Order ID 408-8553883-7805968 Tracking number 513317508899 Product (s) Women's Velvet heavy Sequence Readymade Sleeveless Blouse (38 Size   4inch Margin, red) (1) Return reason C-Returns </div>\r\n\r\n--00000000000080aed005e2d27b4b--\r\n"""


get=(str[(str.find("id") 3):str.find(" id")])

CodePudding user response:

You could use re.findall to find the digits after every order-id #:

import re

str = """order-id #123 order-id #234 order-id #345 order-id #456"""
re.findall(r'order-id #(\d )', str)

Output:

['123', '234', '345', '456']

CodePudding user response:

If you know that your string will always look the same, you can use str.split and just split ( = remove) the parts that you don't need. This will give you a list of the order numbers (NOTE! They will still be strings unless you change type).

str = "order-id #123 order-id #234 order-id #345 order-id #456"

splitted = str.split("order-id #")
orderID = []
for item in splitted:
    if item == "":
        continue
    orderID.append(item)
print(orderID)

Output:

['123 ', '234 ', '345 ', '456']
  • Related