Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Extracting Text
#1
So I have a text file. containing around 400 lines of my Instagram following info like Email:username:posts
I want to extract all the usernames into another text file
Can anyone help me?
Reply
#2
how do you think you might go about this?
do you have sample data that you can share?
Reply
#3
Like:

>>> s = 'Email:username:posts'                                                             
>>> s.split(':')[1]                                                                        
'username'
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
(Nov-13-2019, 05:44 AM)Larz60+ Wrote: how do you think you might go about this?
do you have sample data that you can share?

I have some Idea, but my code is too messy
That's why I'm not asking for correction in code
Reply
#5
(Nov-13-2019, 07:00 AM)Evil_Patrick Wrote: I have some Idea, but my code is too messy
That's why I'm not asking for correction in code


how messy could be 3 lines of code? And it could be 2 lines if you are minimalist
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
(Nov-13-2019, 07:49 AM)buran Wrote:
(Nov-13-2019, 07:00 AM)Evil_Patrick Wrote: I have some Idea, but my code is too messy
That's why I'm not asking for correction in code


how messy could be 3 lines of code? And it could be 2 lines if you are minimalist

This was my code

delimiterInFile = [':']

with open(r'C:\Users\Evil Patrick\Desktop\Insta.txt', 'r') as file:
    listLine = file.readlines()
    for itemLine in listLine:
        item =str(itemLine)
        for delimeter in delimiterInFile:
            item = item.replace(str(delimeter),' ')
            print(item)
I don't know WTF I was tryiny to do
Reply
#7
Here is your code with some comments from me to help. I don't want to give you the solution, as it will be more beneficial to you

delimiterInFile = [':'] # why list? you know it's a semi-colon, no other delimiters
 
with open(r'C:\Users\Evil Patrick\Desktop\Insta.txt', 'r') as file:
    listLine = file.readlines() # you can iterate directly over file, no need to read full file in memory, but this is also ok
    for itemLine in listLine:
        item =str(itemLine) # unnecessary, and itemLine is str anyway
        for delimeter in delimiterInFile: # not necessary
            item = item.replace(str(delimeter),' ') # you don't want to replace, you want to split itemLine at delimiter and take the first relement from resulting list
            print(item)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to remove footer from PDF when extracting to text jh67 3 4,849 Dec-13-2022, 06:52 AM
Last Post: DPaul
  Extracting Specific Lines from text file based on content. jokerfmj 8 2,856 Mar-28-2022, 03:38 PM
Last Post: snippsat
  Extracting all text from a video jehoshua 2 2,147 Nov-14-2021, 09:54 PM
Last Post: jehoshua
  Extracting the text between each "i class" knight2000 4 2,275 May-26-2021, 09:55 AM
Last Post: knight2000
  Extracting data based on specific patterns in a text file K11 1 2,179 Aug-28-2020, 09:00 AM
Last Post: Gribouillis
  Extracting a portion of a text document alarcon032002 8 4,245 Jan-17-2019, 10:35 PM
Last Post: Larz60+
  Google Cloud Vision: Extracting Location of Text pablo_castano 0 2,634 Jun-24-2018, 02:47 AM
Last Post: pablo_castano

Forum Jump:

User Panel Messages

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