move a word to the end of the line using pythonCalling an external command in PythonWhat are metaclasses in Python?Finding the index of an item given a list containing it in PythonHow do I iterate over the words of a string?How can I safely create a nested directory in Python?Does Python have a ternary conditional operator?How to read a file line-by-line into a list?Does Python have a string 'contains' substring method?How do I check if a string contains a specific word?How do I lowercase a string in Python?

Where does the Z80 processor start executing from?

Is there a good way to store credentials outside of a password manager?

Integer addition + constant, is it a group?

What is the difference between "behavior" and "behaviour"?

How do I go from 300 unfinished/half written blog posts, to published posts?

Hostile work environment after whistle-blowing on coworker and our boss. What do I do?

What does the word "Atten" mean?

Is there a problem with hiding "forgot password" until it's needed?

How can I kill an app using Terminal?

Tiptoe or tiphoof? Adjusting words to better fit fantasy races

Is `x >> pure y` equivalent to `liftM (const y) x`

Is expanding the research of a group into machine learning as a PhD student risky?

What happens if you roll doubles 3 times then land on "Go to jail?"

India just shot down a satellite from the ground. At what altitude range is the resulting debris field?

How to be diplomatic in refusing to write code that breaches the privacy of our users

Failed to fetch jessie backports repository

Did Dumbledore lie to Harry about how long he had James Potter's invisibility cloak when he was examining it? If so, why?

How to run a prison with the smallest amount of guards?

How easy is it to start Magic from scratch?

For a non-Jew, is there a punishment for not observing the 7 Noahide Laws?

Do sorcerers' subtle spells require a skill check to be unseen?

Escape a backup date in a file name

Large drywall patch supports

I'm in charge of equipment buying but no one's ever happy with what I choose. How to fix this?



move a word to the end of the line using python


Calling an external command in PythonWhat are metaclasses in Python?Finding the index of an item given a list containing it in PythonHow do I iterate over the words of a string?How can I safely create a nested directory in Python?Does Python have a ternary conditional operator?How to read a file line-by-line into a list?Does Python have a string 'contains' substring method?How do I check if a string contains a specific word?How do I lowercase a string in Python?













3















I'm parsing an HTML and I'm getting a string of Array that I'm trying to clean it and put in into pdf later. At this level, I would like to move all the words started by @X to the end of the line so I could have in the end all the @X aligned.



Hello World @Xabs
Hello World @Xz
Hello World @Xss
Hello World @Xssa
Hello World @Xqq
Hello World @Xsasas


What I would like to have as an output :



Hello World @Xabs
Hello World @Xz
Hello World @Xss
Hello World @Xssa
Hello World @Xqq
Hello World @Xsaxs


Any ideas?



What I have so far:



# encoding=utf8 
import sys
reload(sys)
#import from lxml import html
from bs4 import BeautifulSoup as soup
import re import codecs
sys.setdefaultencoding('utf8')

# Access to the local URL(Html file) f=codecs.open("C:...file.html", 'r')
page = f.read()
f.close()

#html
parsing page_soup = soup(page,"html.parser")
tree = html.fromstring(page) # extract the important arrays of string

a_s= page_soup.find_all("td", "class" :"row_cell")
for a in a_s:
result = a.text.replace("@X","")
print(final_result)









share|improve this question



















  • 1





    What is an end of the line, is this on the terminal? how long is your line?

    – Nobilis
    Mar 8 at 10:50











  • Did you try to solve it on your own? If you've written any code, it would be useful to share with us.

    – David
    Mar 8 at 10:50











  • What's the output format that you want ? An array as well ?

    – Maxouille
    Mar 8 at 10:51











  • @David, for the moment I'm deleting the @X and my problematic is about to shift the word just after @X to the end of the line

    – XdansLaFoule
    Mar 8 at 11:04















3















I'm parsing an HTML and I'm getting a string of Array that I'm trying to clean it and put in into pdf later. At this level, I would like to move all the words started by @X to the end of the line so I could have in the end all the @X aligned.



Hello World @Xabs
Hello World @Xz
Hello World @Xss
Hello World @Xssa
Hello World @Xqq
Hello World @Xsasas


What I would like to have as an output :



Hello World @Xabs
Hello World @Xz
Hello World @Xss
Hello World @Xssa
Hello World @Xqq
Hello World @Xsaxs


Any ideas?



What I have so far:



# encoding=utf8 
import sys
reload(sys)
#import from lxml import html
from bs4 import BeautifulSoup as soup
import re import codecs
sys.setdefaultencoding('utf8')

# Access to the local URL(Html file) f=codecs.open("C:...file.html", 'r')
page = f.read()
f.close()

#html
parsing page_soup = soup(page,"html.parser")
tree = html.fromstring(page) # extract the important arrays of string

a_s= page_soup.find_all("td", "class" :"row_cell")
for a in a_s:
result = a.text.replace("@X","")
print(final_result)









share|improve this question



















  • 1





    What is an end of the line, is this on the terminal? how long is your line?

    – Nobilis
    Mar 8 at 10:50











  • Did you try to solve it on your own? If you've written any code, it would be useful to share with us.

    – David
    Mar 8 at 10:50











  • What's the output format that you want ? An array as well ?

    – Maxouille
    Mar 8 at 10:51











  • @David, for the moment I'm deleting the @X and my problematic is about to shift the word just after @X to the end of the line

    – XdansLaFoule
    Mar 8 at 11:04













3












3








3








I'm parsing an HTML and I'm getting a string of Array that I'm trying to clean it and put in into pdf later. At this level, I would like to move all the words started by @X to the end of the line so I could have in the end all the @X aligned.



Hello World @Xabs
Hello World @Xz
Hello World @Xss
Hello World @Xssa
Hello World @Xqq
Hello World @Xsasas


What I would like to have as an output :



Hello World @Xabs
Hello World @Xz
Hello World @Xss
Hello World @Xssa
Hello World @Xqq
Hello World @Xsaxs


Any ideas?



What I have so far:



# encoding=utf8 
import sys
reload(sys)
#import from lxml import html
from bs4 import BeautifulSoup as soup
import re import codecs
sys.setdefaultencoding('utf8')

# Access to the local URL(Html file) f=codecs.open("C:...file.html", 'r')
page = f.read()
f.close()

#html
parsing page_soup = soup(page,"html.parser")
tree = html.fromstring(page) # extract the important arrays of string

a_s= page_soup.find_all("td", "class" :"row_cell")
for a in a_s:
result = a.text.replace("@X","")
print(final_result)









share|improve this question
















I'm parsing an HTML and I'm getting a string of Array that I'm trying to clean it and put in into pdf later. At this level, I would like to move all the words started by @X to the end of the line so I could have in the end all the @X aligned.



Hello World @Xabs
Hello World @Xz
Hello World @Xss
Hello World @Xssa
Hello World @Xqq
Hello World @Xsasas


What I would like to have as an output :



Hello World @Xabs
Hello World @Xz
Hello World @Xss
Hello World @Xssa
Hello World @Xqq
Hello World @Xsaxs


Any ideas?



What I have so far:



# encoding=utf8 
import sys
reload(sys)
#import from lxml import html
from bs4 import BeautifulSoup as soup
import re import codecs
sys.setdefaultencoding('utf8')

# Access to the local URL(Html file) f=codecs.open("C:...file.html", 'r')
page = f.read()
f.close()

#html
parsing page_soup = soup(page,"html.parser")
tree = html.fromstring(page) # extract the important arrays of string

a_s= page_soup.find_all("td", "class" :"row_cell")
for a in a_s:
result = a.text.replace("@X","")
print(final_result)






python string






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 8 at 11:22









Nobilis

5,3702149




5,3702149










asked Mar 8 at 10:48









XdansLaFouleXdansLaFoule

405




405







  • 1





    What is an end of the line, is this on the terminal? how long is your line?

    – Nobilis
    Mar 8 at 10:50











  • Did you try to solve it on your own? If you've written any code, it would be useful to share with us.

    – David
    Mar 8 at 10:50











  • What's the output format that you want ? An array as well ?

    – Maxouille
    Mar 8 at 10:51











  • @David, for the moment I'm deleting the @X and my problematic is about to shift the word just after @X to the end of the line

    – XdansLaFoule
    Mar 8 at 11:04












  • 1





    What is an end of the line, is this on the terminal? how long is your line?

    – Nobilis
    Mar 8 at 10:50











  • Did you try to solve it on your own? If you've written any code, it would be useful to share with us.

    – David
    Mar 8 at 10:50











  • What's the output format that you want ? An array as well ?

    – Maxouille
    Mar 8 at 10:51











  • @David, for the moment I'm deleting the @X and my problematic is about to shift the word just after @X to the end of the line

    – XdansLaFoule
    Mar 8 at 11:04







1




1





What is an end of the line, is this on the terminal? how long is your line?

– Nobilis
Mar 8 at 10:50





What is an end of the line, is this on the terminal? how long is your line?

– Nobilis
Mar 8 at 10:50













Did you try to solve it on your own? If you've written any code, it would be useful to share with us.

– David
Mar 8 at 10:50





Did you try to solve it on your own? If you've written any code, it would be useful to share with us.

– David
Mar 8 at 10:50













What's the output format that you want ? An array as well ?

– Maxouille
Mar 8 at 10:51





What's the output format that you want ? An array as well ?

– Maxouille
Mar 8 at 10:51













@David, for the moment I'm deleting the @X and my problematic is about to shift the word just after @X to the end of the line

– XdansLaFoule
Mar 8 at 11:04





@David, for the moment I'm deleting the @X and my problematic is about to shift the word just after @X to the end of the line

– XdansLaFoule
Mar 8 at 11:04












2 Answers
2






active

oldest

votes


















1














Quite similar to @blue_note's answer, but making the entire solution more automatical:



import re

lines = ['Hello World @Xabs',
'Hello World @Xz',
'Hello World @Xss',
'Hello World @Xssa',
'Hello World @Xqq',
'Hello World @Xsasas']

aligned_lines = []
for line in lines:
match = re.findall('@Xw+', line)[0]
line = line.replace(match,'')
aligned_lines.append('%-50s %s' % (line, match))

aligned_lines

['Hello World @Xabs',
'Hello World @Xz',
'Hello World @Xss',
'Hello World @Xssa',
'Hello World @Xqq',
'Hello World @Xsasas']





share|improve this answer






























    5














    There is no specific line-width concept in a string. If you want to align your text, print the first part with constant width



    output = ":50s ".format('preceding text', 'Xword')





    share|improve this answer




















    • 1





      I'd not talk about line-width but about line size or constant amount of characters. Talking about width is quite misleading, especially for HTML. Good solution though

      – Nenri
      Mar 8 at 10:57











    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%2f55061589%2fmove-a-word-to-the-end-of-the-line-using-python%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Quite similar to @blue_note's answer, but making the entire solution more automatical:



    import re

    lines = ['Hello World @Xabs',
    'Hello World @Xz',
    'Hello World @Xss',
    'Hello World @Xssa',
    'Hello World @Xqq',
    'Hello World @Xsasas']

    aligned_lines = []
    for line in lines:
    match = re.findall('@Xw+', line)[0]
    line = line.replace(match,'')
    aligned_lines.append('%-50s %s' % (line, match))

    aligned_lines

    ['Hello World @Xabs',
    'Hello World @Xz',
    'Hello World @Xss',
    'Hello World @Xssa',
    'Hello World @Xqq',
    'Hello World @Xsasas']





    share|improve this answer



























      1














      Quite similar to @blue_note's answer, but making the entire solution more automatical:



      import re

      lines = ['Hello World @Xabs',
      'Hello World @Xz',
      'Hello World @Xss',
      'Hello World @Xssa',
      'Hello World @Xqq',
      'Hello World @Xsasas']

      aligned_lines = []
      for line in lines:
      match = re.findall('@Xw+', line)[0]
      line = line.replace(match,'')
      aligned_lines.append('%-50s %s' % (line, match))

      aligned_lines

      ['Hello World @Xabs',
      'Hello World @Xz',
      'Hello World @Xss',
      'Hello World @Xssa',
      'Hello World @Xqq',
      'Hello World @Xsasas']





      share|improve this answer

























        1












        1








        1







        Quite similar to @blue_note's answer, but making the entire solution more automatical:



        import re

        lines = ['Hello World @Xabs',
        'Hello World @Xz',
        'Hello World @Xss',
        'Hello World @Xssa',
        'Hello World @Xqq',
        'Hello World @Xsasas']

        aligned_lines = []
        for line in lines:
        match = re.findall('@Xw+', line)[0]
        line = line.replace(match,'')
        aligned_lines.append('%-50s %s' % (line, match))

        aligned_lines

        ['Hello World @Xabs',
        'Hello World @Xz',
        'Hello World @Xss',
        'Hello World @Xssa',
        'Hello World @Xqq',
        'Hello World @Xsasas']





        share|improve this answer













        Quite similar to @blue_note's answer, but making the entire solution more automatical:



        import re

        lines = ['Hello World @Xabs',
        'Hello World @Xz',
        'Hello World @Xss',
        'Hello World @Xssa',
        'Hello World @Xqq',
        'Hello World @Xsasas']

        aligned_lines = []
        for line in lines:
        match = re.findall('@Xw+', line)[0]
        line = line.replace(match,'')
        aligned_lines.append('%-50s %s' % (line, match))

        aligned_lines

        ['Hello World @Xabs',
        'Hello World @Xz',
        'Hello World @Xss',
        'Hello World @Xssa',
        'Hello World @Xqq',
        'Hello World @Xsasas']






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 8 at 11:09









        ChrisChris

        3,341422




        3,341422























            5














            There is no specific line-width concept in a string. If you want to align your text, print the first part with constant width



            output = ":50s ".format('preceding text', 'Xword')





            share|improve this answer




















            • 1





              I'd not talk about line-width but about line size or constant amount of characters. Talking about width is quite misleading, especially for HTML. Good solution though

              – Nenri
              Mar 8 at 10:57
















            5














            There is no specific line-width concept in a string. If you want to align your text, print the first part with constant width



            output = ":50s ".format('preceding text', 'Xword')





            share|improve this answer




















            • 1





              I'd not talk about line-width but about line size or constant amount of characters. Talking about width is quite misleading, especially for HTML. Good solution though

              – Nenri
              Mar 8 at 10:57














            5












            5








            5







            There is no specific line-width concept in a string. If you want to align your text, print the first part with constant width



            output = ":50s ".format('preceding text', 'Xword')





            share|improve this answer















            There is no specific line-width concept in a string. If you want to align your text, print the first part with constant width



            output = ":50s ".format('preceding text', 'Xword')






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Mar 8 at 12:11

























            answered Mar 8 at 10:54









            blue_noteblue_note

            12k32536




            12k32536







            • 1





              I'd not talk about line-width but about line size or constant amount of characters. Talking about width is quite misleading, especially for HTML. Good solution though

              – Nenri
              Mar 8 at 10:57













            • 1





              I'd not talk about line-width but about line size or constant amount of characters. Talking about width is quite misleading, especially for HTML. Good solution though

              – Nenri
              Mar 8 at 10:57








            1




            1





            I'd not talk about line-width but about line size or constant amount of characters. Talking about width is quite misleading, especially for HTML. Good solution though

            – Nenri
            Mar 8 at 10:57






            I'd not talk about line-width but about line size or constant amount of characters. Talking about width is quite misleading, especially for HTML. Good solution though

            – Nenri
            Mar 8 at 10:57


















            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%2f55061589%2fmove-a-word-to-the-end-of-the-line-using-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 get text form Clipboard with JavaScript in Firefox 56?How to validate an email address in JavaScript?How do JavaScript closures work?How do I remove a property from a JavaScript object?How do you get a timestamp in JavaScript?How do I copy to the clipboard in JavaScript?How do I include a JavaScript file in another JavaScript file?Get the current URL with JavaScript?How to replace all occurrences of a string in JavaScriptHow to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?

            Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

            List of MPs elected to the English parliament in 1640 (April) Contents List of constituencies and members See also Notes References Navigation menueNational Archives – The Glynde Place ArchivesCobbett's Parliamentary history of England, from the Norman Conquest in 1066 to the year 1803'Aldermen in Parliament', The Aldermen of the City of London: Temp. Henry III – 1912onepage&q&f&#61, false 229