How to parse email and passwords from text file (Python)












0















how would I go about extracting emails and passwords from a text file in the format of email:pass ? I would like to attribute email to a variable, and password to a variable as well.










share|improve this question























  • How is the text file structured?

    – FMarazzi
    Nov 13 '18 at 16:46











  • Its in the question. email:pass, then a new line, with another email:pass, until the end of the txt file

    – ScriptKiddie
    Nov 13 '18 at 16:49
















0















how would I go about extracting emails and passwords from a text file in the format of email:pass ? I would like to attribute email to a variable, and password to a variable as well.










share|improve this question























  • How is the text file structured?

    – FMarazzi
    Nov 13 '18 at 16:46











  • Its in the question. email:pass, then a new line, with another email:pass, until the end of the txt file

    – ScriptKiddie
    Nov 13 '18 at 16:49














0












0








0








how would I go about extracting emails and passwords from a text file in the format of email:pass ? I would like to attribute email to a variable, and password to a variable as well.










share|improve this question














how would I go about extracting emails and passwords from a text file in the format of email:pass ? I would like to attribute email to a variable, and password to a variable as well.







python parsing






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 16:44









ScriptKiddieScriptKiddie

22




22













  • How is the text file structured?

    – FMarazzi
    Nov 13 '18 at 16:46











  • Its in the question. email:pass, then a new line, with another email:pass, until the end of the txt file

    – ScriptKiddie
    Nov 13 '18 at 16:49



















  • How is the text file structured?

    – FMarazzi
    Nov 13 '18 at 16:46











  • Its in the question. email:pass, then a new line, with another email:pass, until the end of the txt file

    – ScriptKiddie
    Nov 13 '18 at 16:49

















How is the text file structured?

– FMarazzi
Nov 13 '18 at 16:46





How is the text file structured?

– FMarazzi
Nov 13 '18 at 16:46













Its in the question. email:pass, then a new line, with another email:pass, until the end of the txt file

– ScriptKiddie
Nov 13 '18 at 16:49





Its in the question. email:pass, then a new line, with another email:pass, until the end of the txt file

– ScriptKiddie
Nov 13 '18 at 16:49












4 Answers
4






active

oldest

votes


















0














Assuming the text file has the form of-



email:password
email:password
email:password


and so on, here's how you could extract each piece



afile = open("yourfile.txt", "r')
for line in afile:
pieces = line.split(":")
email = pieces[0]
password = pieces[1]
#do whatever else you need to with each email and password here>


You'll have to make sure that your Python file is in the same directory as the .txt file for this to run correctly as-is (replacing "yourfile.txt" with your actual filename, of course). The only thing to watch out for is if an email address could have a ":" in it. If it does, for example "abc:def@email.com", then "abc" would get set as the email and "def@email.com" would get set as the password. If emails can include colons, then you'd have a bit more work to better split each line. But if they don't include colons, you're set.






share|improve this answer


























  • thanks for the answer. I get Index Error: list index out of range when running this though

    – ScriptKiddie
    Nov 14 '18 at 20:43











  • You can debug by printing out the variable pieces before indexing to get the email and password. That indicates that there's a line that doesn't get correctly split

    – Hollywood
    Nov 14 '18 at 20:49



















0














You're first going to need to figure out how the text file is formatted. If there is different emails on each line then you can use this resource to read each line Read Lines in Python. Once you are able to read the file lines, you need to use string manipulation to get the variables you need






share|improve this answer































    0














    If it is comma separated (ie me@email.com, password), convert the file to a CSV file extension and then this is easily done using Pandas.



    You could then do:



    import pandas as pd
    data = pd.read_csv('filepath/to/the/file.csv')
    email = data.iloc[0, 0]
    password = data.iloc[0, 1]


    I should add that if they are not comma separated, adding



     , sep = '(whatever the separator is)'


    in the brackets for read_csv will make this work for anything.
    For example:



    data = pd.read_csv('filepath/to/the/file.csv', sep = ':')





    share|improve this answer

































      0














      If you have a file like:



      email1:password1
      email2:password2:
      email3:password3


      Then you can create a dictionary using:



      emails = {email : password for email, password in map( lambda x:x.split(':'), open('file.txt', 'r').readlines() )}


      This will allow you to call password values using the email string:



      emails['email1']
      password1





      share|improve this answer























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53285740%2fhow-to-parse-email-and-passwords-from-text-file-python%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        0














        Assuming the text file has the form of-



        email:password
        email:password
        email:password


        and so on, here's how you could extract each piece



        afile = open("yourfile.txt", "r')
        for line in afile:
        pieces = line.split(":")
        email = pieces[0]
        password = pieces[1]
        #do whatever else you need to with each email and password here>


        You'll have to make sure that your Python file is in the same directory as the .txt file for this to run correctly as-is (replacing "yourfile.txt" with your actual filename, of course). The only thing to watch out for is if an email address could have a ":" in it. If it does, for example "abc:def@email.com", then "abc" would get set as the email and "def@email.com" would get set as the password. If emails can include colons, then you'd have a bit more work to better split each line. But if they don't include colons, you're set.






        share|improve this answer


























        • thanks for the answer. I get Index Error: list index out of range when running this though

          – ScriptKiddie
          Nov 14 '18 at 20:43











        • You can debug by printing out the variable pieces before indexing to get the email and password. That indicates that there's a line that doesn't get correctly split

          – Hollywood
          Nov 14 '18 at 20:49
















        0














        Assuming the text file has the form of-



        email:password
        email:password
        email:password


        and so on, here's how you could extract each piece



        afile = open("yourfile.txt", "r')
        for line in afile:
        pieces = line.split(":")
        email = pieces[0]
        password = pieces[1]
        #do whatever else you need to with each email and password here>


        You'll have to make sure that your Python file is in the same directory as the .txt file for this to run correctly as-is (replacing "yourfile.txt" with your actual filename, of course). The only thing to watch out for is if an email address could have a ":" in it. If it does, for example "abc:def@email.com", then "abc" would get set as the email and "def@email.com" would get set as the password. If emails can include colons, then you'd have a bit more work to better split each line. But if they don't include colons, you're set.






        share|improve this answer


























        • thanks for the answer. I get Index Error: list index out of range when running this though

          – ScriptKiddie
          Nov 14 '18 at 20:43











        • You can debug by printing out the variable pieces before indexing to get the email and password. That indicates that there's a line that doesn't get correctly split

          – Hollywood
          Nov 14 '18 at 20:49














        0












        0








        0







        Assuming the text file has the form of-



        email:password
        email:password
        email:password


        and so on, here's how you could extract each piece



        afile = open("yourfile.txt", "r')
        for line in afile:
        pieces = line.split(":")
        email = pieces[0]
        password = pieces[1]
        #do whatever else you need to with each email and password here>


        You'll have to make sure that your Python file is in the same directory as the .txt file for this to run correctly as-is (replacing "yourfile.txt" with your actual filename, of course). The only thing to watch out for is if an email address could have a ":" in it. If it does, for example "abc:def@email.com", then "abc" would get set as the email and "def@email.com" would get set as the password. If emails can include colons, then you'd have a bit more work to better split each line. But if they don't include colons, you're set.






        share|improve this answer















        Assuming the text file has the form of-



        email:password
        email:password
        email:password


        and so on, here's how you could extract each piece



        afile = open("yourfile.txt", "r')
        for line in afile:
        pieces = line.split(":")
        email = pieces[0]
        password = pieces[1]
        #do whatever else you need to with each email and password here>


        You'll have to make sure that your Python file is in the same directory as the .txt file for this to run correctly as-is (replacing "yourfile.txt" with your actual filename, of course). The only thing to watch out for is if an email address could have a ":" in it. If it does, for example "abc:def@email.com", then "abc" would get set as the email and "def@email.com" would get set as the password. If emails can include colons, then you'd have a bit more work to better split each line. But if they don't include colons, you're set.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 13 '18 at 16:56

























        answered Nov 13 '18 at 16:51









        HollywoodHollywood

        9411




        9411













        • thanks for the answer. I get Index Error: list index out of range when running this though

          – ScriptKiddie
          Nov 14 '18 at 20:43











        • You can debug by printing out the variable pieces before indexing to get the email and password. That indicates that there's a line that doesn't get correctly split

          – Hollywood
          Nov 14 '18 at 20:49



















        • thanks for the answer. I get Index Error: list index out of range when running this though

          – ScriptKiddie
          Nov 14 '18 at 20:43











        • You can debug by printing out the variable pieces before indexing to get the email and password. That indicates that there's a line that doesn't get correctly split

          – Hollywood
          Nov 14 '18 at 20:49

















        thanks for the answer. I get Index Error: list index out of range when running this though

        – ScriptKiddie
        Nov 14 '18 at 20:43





        thanks for the answer. I get Index Error: list index out of range when running this though

        – ScriptKiddie
        Nov 14 '18 at 20:43













        You can debug by printing out the variable pieces before indexing to get the email and password. That indicates that there's a line that doesn't get correctly split

        – Hollywood
        Nov 14 '18 at 20:49





        You can debug by printing out the variable pieces before indexing to get the email and password. That indicates that there's a line that doesn't get correctly split

        – Hollywood
        Nov 14 '18 at 20:49













        0














        You're first going to need to figure out how the text file is formatted. If there is different emails on each line then you can use this resource to read each line Read Lines in Python. Once you are able to read the file lines, you need to use string manipulation to get the variables you need






        share|improve this answer




























          0














          You're first going to need to figure out how the text file is formatted. If there is different emails on each line then you can use this resource to read each line Read Lines in Python. Once you are able to read the file lines, you need to use string manipulation to get the variables you need






          share|improve this answer


























            0












            0








            0







            You're first going to need to figure out how the text file is formatted. If there is different emails on each line then you can use this resource to read each line Read Lines in Python. Once you are able to read the file lines, you need to use string manipulation to get the variables you need






            share|improve this answer













            You're first going to need to figure out how the text file is formatted. If there is different emails on each line then you can use this resource to read each line Read Lines in Python. Once you are able to read the file lines, you need to use string manipulation to get the variables you need







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 13 '18 at 16:55









            BrandalfBrandalf

            503




            503























                0














                If it is comma separated (ie me@email.com, password), convert the file to a CSV file extension and then this is easily done using Pandas.



                You could then do:



                import pandas as pd
                data = pd.read_csv('filepath/to/the/file.csv')
                email = data.iloc[0, 0]
                password = data.iloc[0, 1]


                I should add that if they are not comma separated, adding



                 , sep = '(whatever the separator is)'


                in the brackets for read_csv will make this work for anything.
                For example:



                data = pd.read_csv('filepath/to/the/file.csv', sep = ':')





                share|improve this answer






























                  0














                  If it is comma separated (ie me@email.com, password), convert the file to a CSV file extension and then this is easily done using Pandas.



                  You could then do:



                  import pandas as pd
                  data = pd.read_csv('filepath/to/the/file.csv')
                  email = data.iloc[0, 0]
                  password = data.iloc[0, 1]


                  I should add that if they are not comma separated, adding



                   , sep = '(whatever the separator is)'


                  in the brackets for read_csv will make this work for anything.
                  For example:



                  data = pd.read_csv('filepath/to/the/file.csv', sep = ':')





                  share|improve this answer




























                    0












                    0








                    0







                    If it is comma separated (ie me@email.com, password), convert the file to a CSV file extension and then this is easily done using Pandas.



                    You could then do:



                    import pandas as pd
                    data = pd.read_csv('filepath/to/the/file.csv')
                    email = data.iloc[0, 0]
                    password = data.iloc[0, 1]


                    I should add that if they are not comma separated, adding



                     , sep = '(whatever the separator is)'


                    in the brackets for read_csv will make this work for anything.
                    For example:



                    data = pd.read_csv('filepath/to/the/file.csv', sep = ':')





                    share|improve this answer















                    If it is comma separated (ie me@email.com, password), convert the file to a CSV file extension and then this is easily done using Pandas.



                    You could then do:



                    import pandas as pd
                    data = pd.read_csv('filepath/to/the/file.csv')
                    email = data.iloc[0, 0]
                    password = data.iloc[0, 1]


                    I should add that if they are not comma separated, adding



                     , sep = '(whatever the separator is)'


                    in the brackets for read_csv will make this work for anything.
                    For example:



                    data = pd.read_csv('filepath/to/the/file.csv', sep = ':')






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 13 '18 at 16:55

























                    answered Nov 13 '18 at 16:48









                    Fishbones78Fishbones78

                    54




                    54























                        0














                        If you have a file like:



                        email1:password1
                        email2:password2:
                        email3:password3


                        Then you can create a dictionary using:



                        emails = {email : password for email, password in map( lambda x:x.split(':'), open('file.txt', 'r').readlines() )}


                        This will allow you to call password values using the email string:



                        emails['email1']
                        password1





                        share|improve this answer




























                          0














                          If you have a file like:



                          email1:password1
                          email2:password2:
                          email3:password3


                          Then you can create a dictionary using:



                          emails = {email : password for email, password in map( lambda x:x.split(':'), open('file.txt', 'r').readlines() )}


                          This will allow you to call password values using the email string:



                          emails['email1']
                          password1





                          share|improve this answer


























                            0












                            0








                            0







                            If you have a file like:



                            email1:password1
                            email2:password2:
                            email3:password3


                            Then you can create a dictionary using:



                            emails = {email : password for email, password in map( lambda x:x.split(':'), open('file.txt', 'r').readlines() )}


                            This will allow you to call password values using the email string:



                            emails['email1']
                            password1





                            share|improve this answer













                            If you have a file like:



                            email1:password1
                            email2:password2:
                            email3:password3


                            Then you can create a dictionary using:



                            emails = {email : password for email, password in map( lambda x:x.split(':'), open('file.txt', 'r').readlines() )}


                            This will allow you to call password values using the email string:



                            emails['email1']
                            password1






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 13 '18 at 17:03









                            asheetsasheets

                            332213




                            332213






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53285740%2fhow-to-parse-email-and-passwords-from-text-file-python%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Full-time equivalent

                                Bicuculline

                                さくらももこ