Jakub
Jakub

Reputation: 699

Change position of number in text file - first to second second to first in Python?

Hi I have a problem I want to change numbers positions. First number will be second number, second will be first, third will be forth and forth will be third etc. This is small fragment of my input

   1   20  135  154  269  288  403  422  537  556  671  690  805  824  939
 958 1073 1092 1207 1226 1341 1360 1475 1494 1609 1628 1743 1762 1877 1896
2011 2030 2145 2164 2279 2298 2413 2432 2547 2566 2681 2700 2815 2834 2949
2968 3083 3102 3217 3236 3351 3370 3485 3504 3619 3638 3753 3772 3887 3906
4021 4040 4155 4174 4289 4308 4423 4442 4557 4576 4691 4710 4825 4844 4959

So the expected output will be

  20    1  154  135  288  269 etc

But the problem is that, when I try to put my number into list I have something like double space or \n etc I write this

my_file = open("PN_input.txt", "r")
content = my_file.read()
content_list = content.split(" ")
my_file.close()
print(content_list)

and I get something like that, so I am not be able to go to the next step and change the position by using for example for loop

['', '', '', '1', '', '', '20', '', '135', '', '154', '', '269', '', '288', '', '403', '', '422', '', '537', '', '556', '', '671', '', '690', '', '805', '', '824', '', '939\n', '958',

Upvotes: 0

Views: 178

Answers (4)

Sebastian Loehner
Sebastian Loehner

Reputation: 1322

here is something to get you started:


s = """\
   1   20  135  154  269  288  403  422  537  556  671  690  805  824  939
 958 1073 1092 1207 1226 1341 1360 1475 1494 1609 1628 1743 1762 1877 1896
2011 2030 2145 2164 2279 2298 2413 2432 2547 2566 2681 2700 2815 2834 2949
2968 3083 3102 3217 3236 3351 3370 3485 3504 3619 3638 3753 3772 3887 3906
4021 4040 4155 4174 4289 4308 4423 4442 4557 4576 4691 4710 4825 4844 4959
"""

lines = (line.split() for line in s.splitlines())
new_lines = [[line[1], line[0], *line[2:]] for line in lines]

for line in new_lines:
    print(line)

# prints:
# ['20', '1', '135', '154', '269', '288', '403', '422', '537', '556', '671', '690', '805', '824', '939']
# ['1073', '958', '1092', '1207', '1226', '1341', '1360', '1475', '1494', '1609', '1628', '1743', '1762', '1877', '1896']
# ['2030', '2011', '2145', '2164', '2279', '2298', '2413', '2432', '2547', '2566', '2681', '2700', '2815', '2834', '2949']
# ['3083', '2968', '3102', '3217', '3236', '3351', '3370', '3485', '3504', '3619', '3638', '3753', '3772', '3887', '3906']
# ['4040', '4021', '4155', '4174', '4289', '4308', '4423', '4442', '4557', '4576', '4691', '4710', '4825', '4844', '4959']

Upvotes: 0

user5386938
user5386938

Reputation:

As part of my playing around with pandas...

import pandas as pd
from io import StringIO

data = '''\
   1   20  135  154  269  288  403  422  537  556  671  690  805  824  939
 958 1073 1092 1207 1226 1341 1360 1475 1494 1609 1628 1743 1762 1877 1896
2011 2030 2145 2164 2279 2298 2413 2432 2547 2566 2681 2700 2815 2834 2949
2968 3083 3102 3217 3236 3351 3370 3485 3504 3619 3638 3753 3772 3887 3906
4021 4040 4155 4174 4289 4308 4423 4442 4557 4576 4691 4710 4825 4844 4959
'''

cols = 'abcdefghijklmno'
out = list('badcfehgjilknmo')
widths = [4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5]

buff = StringIO()
for w, c in zip(widths, cols):
    buff.write('%*s' % (w, c))
buff.write('\n')
buff.write(data)
buff.seek(0)

df = pd.read_fwf(buff, widths=widths)

swapped = df[out]

buff = StringIO()
swapped.to_string(buff, header=False, index=False)
print(buff.getvalue())

prints out

   20     1   154   135   288   269   422   403   556   537   690   671   824   805   939
 1073   958  1207  1092  1341  1226  1475  1360  1609  1494  1743  1628  1877  1762  1896
 2030  2011  2164  2145  2298  2279  2432  2413  2566  2547  2700  2681  2834  2815  2949
 3083  2968  3217  3102  3351  3236  3485  3370  3619  3504  3753  3638  3887  3772  3906
 4040  4021  4174  4155  4308  4289  4442  4423  4576  4557  4710  4691  4844  4825  4959

Upvotes: 1

Jakub
Jakub

Reputation: 699

I did this. This is my script. If you have some advice how to write it better, please comment. Thanks to all for the advice.

my_file = open("PN_input.txt", "r")
content = my_file.read()
content_list = content.split()
print(len(content_list))
for i in range(0,400,2):
    var1 = content_list[i]
    var2 = content_list[i+1]
    content_list[i] = var2
    content_list[i+1] = var1
    print(var1)
    print(var2)
    print(content_list[i])
    print(content_list[i+1])    
    if i == 400:
        break;
print(content_list)
my_file.close()

with open('out.txt', 'w') as f:
    for item in content_list:
        f.write("%s " % item)
    f.close

Upvotes: 0

jcf
jcf

Reputation: 602

One option would be to clean the list so as to preserve only numeric entries, and clean the ones like '939\n':

my_file = open("PN_input.txt", "r")
content = my_file.read()
content_list = content.split(" ")
my_file.close()

new_content_list = []
non_allowed_chars = (' ', '\n', '\t') 
for e in content_list:
    new_string = e
    for character in non_allowed_chars: # replace non-allowed chars for empty string
        new_string = new_string.replace(character, "") 
    if new_string.isalnum(): # checks if string is alphanumeric
        new_content_list.append(new_string)

print(new_content_list)

As a side note, you should open the file using the with command so as to let python handle the opening and closure of the file:

with open("PN_input.txt", "r") as my_file:
    content = my_file.read()
    content_list = content.split(" ")

Upvotes: 0

Related Questions