Python Forum
Extract only certain text which are needed
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Extract only certain text which are needed
#25
Maybe this?

with open ('data', 'r', ) as f:
    content = 'start'
    while content:
        content = f.readline()
        temp = content.split(',')
        amount = email = mobile = accountOwner = ''
        for item in temp:
            if 'amount' in item:
                amount = item
            elif 'email' in item:
                email = item
            elif 'mobile' in item:
                mobile = item
            elif 'accountOwner' in item:
                accountOwner = item
            if amount and email and mobile and accountOwner:
                print(f"{amount} {email} {mobile} {accountOwner}")
                amount = email = mobile = accountOwner = ''
Output:
"amount":300 "email":"[email protected]" "mobile":"100000012457" "accountOwner":"Tom Hank" "amount":500 "email":"[email protected]" "mobile":"7845147210" "accountOwner":"Cotton Kate" "amount":300 "email":"[email protected]" "mobile":"1457845478" "accountOwner":"Stefen James" "amount":100 "email":"[email protected]" "mobile":"1457812014" "accountOwner":"Michel" "amount":100 "email":"[email protected]" "mobile":"4578412457" "accountOwner":"Tom Willims"
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply


Messages In This Thread
RE: Extract only certain text which are needed - by rob101 - Oct-10-2022, 09:54 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  extract only text strip byte array Pir8Radio 7 3,090 Nov-29-2022, 10:24 PM
Last Post: Pir8Radio
  Extract text rektcol 6 1,758 Jun-28-2022, 08:57 AM
Last Post: Gribouillis
  Extract a string between 2 words from a text file OscarBoots 2 1,918 Nov-02-2021, 08:50 AM
Last Post: ibreeden
  Extract text based on postion and pattern guddu_12 2 1,686 Sep-27-2021, 08:32 PM
Last Post: guddu_12
  Extract specific sentences from text file Bubly 3 3,491 May-31-2021, 06:55 PM
Last Post: Larz60+
  extract color text from PDF Maha 0 2,109 May-31-2021, 04:05 PM
Last Post: Maha
Question How to extract multiple text from a string? chatguy 2 2,434 Feb-28-2021, 07:39 AM
Last Post: bowlofred
  How to extract a single word from a text file buttercup 7 3,722 Jul-22-2020, 04:45 AM
Last Post: bowlofred
  How to extract specific rows and columns from a text file with Python Farhan 0 3,437 Mar-25-2020, 09:18 PM
Last Post: Farhan
  Extract Strings From Text File - Out Put Results to Individual Files dj99 8 5,032 Jun-28-2018, 10:41 AM
Last Post: dj99

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020