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
#18
(Oct-10-2022, 07:58 AM)rob101 Wrote:
(Oct-10-2022, 03:55 AM)Calli Wrote: It looks like it's printing out the last line of the file

(Oct-10-2022, 07:06 AM)ibreeden Wrote: Is'nt that what you wanted? You want more? Then add 4 spaces before the print function.

Indeed: move the print() function into the for loop.

Traceback (most recent call last):
  File "/media/redhat/test/data/df.py", line 15, in <module>
    print(amount,email,mobile,accountOwner)
NameError: name 'amount' is not defined
with open ('df.json', 'r') as f:
    content = f.read()

temp = content.split(',')

for item in temp:
    if 'amount' in item:
        amount = item.strip()
    elif 'email' in item:
        email = item.strip()
    elif 'mobile' in item:
        mobile = item.strip()
    elif 'accountOwner' in item:
        accountOwner = item.strip()
    print(amount,email,mobile,accountOwner)
Reply


Messages In This Thread
RE: Extract only certain text which are needed - by Calli - Oct-10-2022, 08:02 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  extract only text strip byte array Pir8Radio 7 3,442 Nov-29-2022, 10:24 PM
Last Post: Pir8Radio
  Extract text rektcol 6 1,954 Jun-28-2022, 08:57 AM
Last Post: Gribouillis
  Extract a string between 2 words from a text file OscarBoots 2 1,996 Nov-02-2021, 08:50 AM
Last Post: ibreeden
  Extract text based on postion and pattern guddu_12 2 1,772 Sep-27-2021, 08:32 PM
Last Post: guddu_12
  Extract specific sentences from text file Bubly 3 3,646 May-31-2021, 06:55 PM
Last Post: Larz60+
  extract color text from PDF Maha 0 2,164 May-31-2021, 04:05 PM
Last Post: Maha
Question How to extract multiple text from a string? chatguy 2 2,548 Feb-28-2021, 07:39 AM
Last Post: bowlofred
  How to extract a single word from a text file buttercup 7 3,965 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,513 Mar-25-2020, 09:18 PM
Last Post: Farhan
  Extract Strings From Text File - Out Put Results to Individual Files dj99 8 5,181 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