Better way to extract only 2nd column of a txt file in python?












0














I have a txt file that looks like this. The number part and sentence part is separated by one tab.



1234  I'll give 5.
1334 Surprisingly well made.


I'm trying to only extract second column (sentence part) and put it into a variable. I could do this by using .split() and .join() as below



f = open('test.txt', 'r', encoding='utf8')
for line in f.readlines():
temp = line.split()
del temp[0]
line = ' '.join(temp)
print(line)


Just thought there might be better way to do this that doesn't do
split and join which looks kind of meaningless.. like is there anyway to group up the rest part once it hits 'tab'?










share|improve this question






















  • Why not just line = line.split()[1]?
    – slider
    Nov 19 '18 at 2:14










  • .split('t', 1)?
    – Klaus D.
    Nov 19 '18 at 2:15












  • @KlausD. That captures the first part (numbers) too.
    – PuffedRiceCrackers
    Nov 19 '18 at 2:25










  • @slider That extracts only the first word of each sentence.
    – PuffedRiceCrackers
    Nov 19 '18 at 2:26
















0














I have a txt file that looks like this. The number part and sentence part is separated by one tab.



1234  I'll give 5.
1334 Surprisingly well made.


I'm trying to only extract second column (sentence part) and put it into a variable. I could do this by using .split() and .join() as below



f = open('test.txt', 'r', encoding='utf8')
for line in f.readlines():
temp = line.split()
del temp[0]
line = ' '.join(temp)
print(line)


Just thought there might be better way to do this that doesn't do
split and join which looks kind of meaningless.. like is there anyway to group up the rest part once it hits 'tab'?










share|improve this question






















  • Why not just line = line.split()[1]?
    – slider
    Nov 19 '18 at 2:14










  • .split('t', 1)?
    – Klaus D.
    Nov 19 '18 at 2:15












  • @KlausD. That captures the first part (numbers) too.
    – PuffedRiceCrackers
    Nov 19 '18 at 2:25










  • @slider That extracts only the first word of each sentence.
    – PuffedRiceCrackers
    Nov 19 '18 at 2:26














0












0








0







I have a txt file that looks like this. The number part and sentence part is separated by one tab.



1234  I'll give 5.
1334 Surprisingly well made.


I'm trying to only extract second column (sentence part) and put it into a variable. I could do this by using .split() and .join() as below



f = open('test.txt', 'r', encoding='utf8')
for line in f.readlines():
temp = line.split()
del temp[0]
line = ' '.join(temp)
print(line)


Just thought there might be better way to do this that doesn't do
split and join which looks kind of meaningless.. like is there anyway to group up the rest part once it hits 'tab'?










share|improve this question













I have a txt file that looks like this. The number part and sentence part is separated by one tab.



1234  I'll give 5.
1334 Surprisingly well made.


I'm trying to only extract second column (sentence part) and put it into a variable. I could do this by using .split() and .join() as below



f = open('test.txt', 'r', encoding='utf8')
for line in f.readlines():
temp = line.split()
del temp[0]
line = ' '.join(temp)
print(line)


Just thought there might be better way to do this that doesn't do
split and join which looks kind of meaningless.. like is there anyway to group up the rest part once it hits 'tab'?







python python-3.x file-io






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 '18 at 2:11









PuffedRiceCrackersPuffedRiceCrackers

536




536












  • Why not just line = line.split()[1]?
    – slider
    Nov 19 '18 at 2:14










  • .split('t', 1)?
    – Klaus D.
    Nov 19 '18 at 2:15












  • @KlausD. That captures the first part (numbers) too.
    – PuffedRiceCrackers
    Nov 19 '18 at 2:25










  • @slider That extracts only the first word of each sentence.
    – PuffedRiceCrackers
    Nov 19 '18 at 2:26


















  • Why not just line = line.split()[1]?
    – slider
    Nov 19 '18 at 2:14










  • .split('t', 1)?
    – Klaus D.
    Nov 19 '18 at 2:15












  • @KlausD. That captures the first part (numbers) too.
    – PuffedRiceCrackers
    Nov 19 '18 at 2:25










  • @slider That extracts only the first word of each sentence.
    – PuffedRiceCrackers
    Nov 19 '18 at 2:26
















Why not just line = line.split()[1]?
– slider
Nov 19 '18 at 2:14




Why not just line = line.split()[1]?
– slider
Nov 19 '18 at 2:14












.split('t', 1)?
– Klaus D.
Nov 19 '18 at 2:15






.split('t', 1)?
– Klaus D.
Nov 19 '18 at 2:15














@KlausD. That captures the first part (numbers) too.
– PuffedRiceCrackers
Nov 19 '18 at 2:25




@KlausD. That captures the first part (numbers) too.
– PuffedRiceCrackers
Nov 19 '18 at 2:25












@slider That extracts only the first word of each sentence.
– PuffedRiceCrackers
Nov 19 '18 at 2:26




@slider That extracts only the first word of each sentence.
– PuffedRiceCrackers
Nov 19 '18 at 2:26












3 Answers
3






active

oldest

votes


















2














Or kinda easier:



with open('test.txt', 'r', encoding='utf8') as f:
print('n'.join(line.split()[1] for line in f))





share|improve this answer

















  • 1




    This is the best answer, but I'll leave mine up as reference
    – Charles Landau
    Nov 19 '18 at 2:26



















1














You could slice and use a context manager so your file handler is not orphaned in edge cases.



with open('test.txt', 'r', encoding='utf8') as f
for line in f:
print((' '.join(line.split()[1:]))





share|improve this answer





















  • Thanks, but it separates every word in a sentence like mine. (something like ['Surprisingly', 'well', 'made.'])
    – PuffedRiceCrackers
    Nov 19 '18 at 2:29










  • I think you read an earlier iteration of my answer @PuffedRiceCrackers, because I believe I fixed that
    – Charles Landau
    Nov 19 '18 at 2:30



















1














Assuming each line always has the second part:



with open('test.txt', 'r', encoding='utf8') as f:
for line in f:
print(line.split('t', 1)[1])


If it does not (then skip it):



with open('test.txt', 'r', encoding='utf8') as f:
for line in f:
try:
print(line.split('t', 1)[1])
except IndexError:
pass





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%2f53367424%2fbetter-way-to-extract-only-2nd-column-of-a-txt-file-in-python%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    Or kinda easier:



    with open('test.txt', 'r', encoding='utf8') as f:
    print('n'.join(line.split()[1] for line in f))





    share|improve this answer

















    • 1




      This is the best answer, but I'll leave mine up as reference
      – Charles Landau
      Nov 19 '18 at 2:26
















    2














    Or kinda easier:



    with open('test.txt', 'r', encoding='utf8') as f:
    print('n'.join(line.split()[1] for line in f))





    share|improve this answer

















    • 1




      This is the best answer, but I'll leave mine up as reference
      – Charles Landau
      Nov 19 '18 at 2:26














    2












    2








    2






    Or kinda easier:



    with open('test.txt', 'r', encoding='utf8') as f:
    print('n'.join(line.split()[1] for line in f))





    share|improve this answer












    Or kinda easier:



    with open('test.txt', 'r', encoding='utf8') as f:
    print('n'.join(line.split()[1] for line in f))






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 19 '18 at 2:25









    U9-ForwardU9-Forward

    13.6k21337




    13.6k21337








    • 1




      This is the best answer, but I'll leave mine up as reference
      – Charles Landau
      Nov 19 '18 at 2:26














    • 1




      This is the best answer, but I'll leave mine up as reference
      – Charles Landau
      Nov 19 '18 at 2:26








    1




    1




    This is the best answer, but I'll leave mine up as reference
    – Charles Landau
    Nov 19 '18 at 2:26




    This is the best answer, but I'll leave mine up as reference
    – Charles Landau
    Nov 19 '18 at 2:26













    1














    You could slice and use a context manager so your file handler is not orphaned in edge cases.



    with open('test.txt', 'r', encoding='utf8') as f
    for line in f:
    print((' '.join(line.split()[1:]))





    share|improve this answer





















    • Thanks, but it separates every word in a sentence like mine. (something like ['Surprisingly', 'well', 'made.'])
      – PuffedRiceCrackers
      Nov 19 '18 at 2:29










    • I think you read an earlier iteration of my answer @PuffedRiceCrackers, because I believe I fixed that
      – Charles Landau
      Nov 19 '18 at 2:30
















    1














    You could slice and use a context manager so your file handler is not orphaned in edge cases.



    with open('test.txt', 'r', encoding='utf8') as f
    for line in f:
    print((' '.join(line.split()[1:]))





    share|improve this answer





















    • Thanks, but it separates every word in a sentence like mine. (something like ['Surprisingly', 'well', 'made.'])
      – PuffedRiceCrackers
      Nov 19 '18 at 2:29










    • I think you read an earlier iteration of my answer @PuffedRiceCrackers, because I believe I fixed that
      – Charles Landau
      Nov 19 '18 at 2:30














    1












    1








    1






    You could slice and use a context manager so your file handler is not orphaned in edge cases.



    with open('test.txt', 'r', encoding='utf8') as f
    for line in f:
    print((' '.join(line.split()[1:]))





    share|improve this answer












    You could slice and use a context manager so your file handler is not orphaned in edge cases.



    with open('test.txt', 'r', encoding='utf8') as f
    for line in f:
    print((' '.join(line.split()[1:]))






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 19 '18 at 2:15









    Charles LandauCharles Landau

    2,0651215




    2,0651215












    • Thanks, but it separates every word in a sentence like mine. (something like ['Surprisingly', 'well', 'made.'])
      – PuffedRiceCrackers
      Nov 19 '18 at 2:29










    • I think you read an earlier iteration of my answer @PuffedRiceCrackers, because I believe I fixed that
      – Charles Landau
      Nov 19 '18 at 2:30


















    • Thanks, but it separates every word in a sentence like mine. (something like ['Surprisingly', 'well', 'made.'])
      – PuffedRiceCrackers
      Nov 19 '18 at 2:29










    • I think you read an earlier iteration of my answer @PuffedRiceCrackers, because I believe I fixed that
      – Charles Landau
      Nov 19 '18 at 2:30
















    Thanks, but it separates every word in a sentence like mine. (something like ['Surprisingly', 'well', 'made.'])
    – PuffedRiceCrackers
    Nov 19 '18 at 2:29




    Thanks, but it separates every word in a sentence like mine. (something like ['Surprisingly', 'well', 'made.'])
    – PuffedRiceCrackers
    Nov 19 '18 at 2:29












    I think you read an earlier iteration of my answer @PuffedRiceCrackers, because I believe I fixed that
    – Charles Landau
    Nov 19 '18 at 2:30




    I think you read an earlier iteration of my answer @PuffedRiceCrackers, because I believe I fixed that
    – Charles Landau
    Nov 19 '18 at 2:30











    1














    Assuming each line always has the second part:



    with open('test.txt', 'r', encoding='utf8') as f:
    for line in f:
    print(line.split('t', 1)[1])


    If it does not (then skip it):



    with open('test.txt', 'r', encoding='utf8') as f:
    for line in f:
    try:
    print(line.split('t', 1)[1])
    except IndexError:
    pass





    share|improve this answer


























      1














      Assuming each line always has the second part:



      with open('test.txt', 'r', encoding='utf8') as f:
      for line in f:
      print(line.split('t', 1)[1])


      If it does not (then skip it):



      with open('test.txt', 'r', encoding='utf8') as f:
      for line in f:
      try:
      print(line.split('t', 1)[1])
      except IndexError:
      pass





      share|improve this answer
























        1












        1








        1






        Assuming each line always has the second part:



        with open('test.txt', 'r', encoding='utf8') as f:
        for line in f:
        print(line.split('t', 1)[1])


        If it does not (then skip it):



        with open('test.txt', 'r', encoding='utf8') as f:
        for line in f:
        try:
        print(line.split('t', 1)[1])
        except IndexError:
        pass





        share|improve this answer












        Assuming each line always has the second part:



        with open('test.txt', 'r', encoding='utf8') as f:
        for line in f:
        print(line.split('t', 1)[1])


        If it does not (then skip it):



        with open('test.txt', 'r', encoding='utf8') as f:
        for line in f:
        try:
        print(line.split('t', 1)[1])
        except IndexError:
        pass






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 '18 at 2:30









        DYZDYZ

        25.8k61948




        25.8k61948






























            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%2f53367424%2fbetter-way-to-extract-only-2nd-column-of-a-txt-file-in-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

            How to change which sound is reproduced for terminal bell?

            Can I use Tabulator js library in my java Spring + Thymeleaf project?

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents