Python Forum
Move a character left or right in a file.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Move a character left or right in a file.
#1
Depending on an input ('a' or 'd') I would like to move a character left or right. I already have this:
pos = line.index('4')
pos = pos - 1
new = new + '1'
for i in range(0, pos-1):
    new = new + '0'
new = new + '4'
for j in range(0, pos+1):
    new = new + '0'
new = new + '1'
FYI: Line is '100000040000001'

When I run this code, new becomes: '1000004000001'. This is not the right length. It you use
len()
it returns '13' not '15' like the original. So it should be: '100000400000001' which is still 15 in length.

Hope it makes sense,
Dream.

EDIT: removed dupe post lines
Reply
#2
Sorry, deleted the other post.

You're not taking into account the length of the string. That is, pos only tells you how wide the left side should be, and you are setting both the left and the right side based on that. So the further left you move, the shorter the string gets, and the further right you move the longer the string gets.

I would fill in the right side by adding 0's until len(new) == len(line) - 1, and then adding a 1.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Mar-21-2019, 06:21 PM)ichabod801 Wrote: Sorry, deleted the other post.

You're not taking into account the length of the string. That is, pos only tells you how wide the left side should be, and you are setting both the left and the right side based on that. So the further left you move, the shorter the string gets, and the further right you move the longer the string gets.

I would fill in the right side by adding 0's until len(new) == len(line) - 1, and then adding a 1.

Dont worry!

Thanks for the reply, I can't try it out now, but I can soon. Thinking about it, it should work.

Dream
Reply
#4
You can just swap the two values instead of iterating over the string.

    line='100000040000001'
    pos = line.index('4')

    ## move to the left
    line_as_list=list(line)
    if pos > 0:
        new_pos=pos - 1  ## move to right= pos + 1
        save_this=line_as_list[new_pos]
        line_as_list[new_pos]='4'
        line_as_list[pos]=save_this

    ## print one above the other for easy comparison
    print(line)
    print("".join(line_as_list)) 
Reply
#5
It works, thanks!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,367 Sep-27-2022, 01:38 PM
Last Post: buran
  PyQT5 - align left frohr 7 3,937 May-07-2022, 09:56 PM
Last Post: deanhystad
  How did one column get left-justified? Mark17 6 1,927 Feb-26-2022, 11:55 PM
Last Post: deanhystad
  [solved] unexpected character after line continuation character paul18fr 4 3,389 Jun-22-2021, 03:22 PM
Last Post: deanhystad
  Move file from one folder to another folder with timestamp added end of file shantanu97 0 2,466 Mar-22-2021, 10:59 AM
Last Post: shantanu97
  Explanation of the left side of this statement please rascalsailor 3 2,493 Sep-09-2020, 02:02 PM
Last Post: rascalsailor
  SyntaxError: unexpected character after line continuation character siteshkumar 2 3,162 Jul-13-2020, 07:05 PM
Last Post: snippsat
  How to left align logging messages Mekala 3 6,817 Jun-28-2020, 04:04 PM
Last Post: bowlofred
  Shutil move if file exists in destination Friend 2 6,752 Feb-02-2020, 01:45 PM
Last Post: Friend
  how can i handle "expected a character " type error , when I input no character vivekagrey 2 2,732 Jan-05-2020, 11:50 AM
Last Post: vivekagrey

Forum Jump:

User Panel Messages

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