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

            Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

            Understanding generators in Python2019 Community Moderator ElectionGenerator function not working pythonFor loop not executing two timesGenerators - Printing generated valuesWhy can a python generator only be used once?What exactly do generators do?What does the “yield” keyword do?What does “list comprehension” mean? How does it work and how can I use it?sklearn Kfold acces single fold instead of for loopIs a generator the callable? Which is the generator?Apply Border To Range Of Cells Using OpenpyxlCalling an external command in PythonWhat are metaclasses in Python?What is the difference between @staticmethod and @classmethod?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?Understanding slice notationUnderstanding Python super() with __init__() methodsDoes Python have a string 'contains' substring method?

            How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page