Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
writing to file
#1
Huh newbie to this!
Trying to write to a new text-file, formatted in a specific way:
The first, source, text-file is containing a list of nubmers in columns.

000123456
000123457
000123458
000123459
000123460
000123461
...........

I want to create a new list looking like this:
starts with parenthesis ("first row number" == "next row of number") and.
Like this:
(000123456 == 000123457) and
(000123458 == 000123459) and
(000123460 == 000123461).........

What is the easiest way to solve this? Started like this:

infile = open("regina.txt", "r+")
outfile = open("Regina_ID.txt", "w")
for line in infile:
infile = ( line + " == " )
outfile.write(infile)
outfile.close()

Thanks in advance!
Reply
#2
You can do something like this:
lines = infile.readlines() #put all the lines in an array
sizeOfGroups = 2
if len(lines) % sizeOfGroups == 1: #if it's an odd number of elements
    lines.remove(lines[-1]) #Remove the last element - you could always add a new element to the end intead of removing.
groups = [lines[n:n+sizeOfGroups] for n in range(0, len(lines), sizeOfGroups)] #use a list comprehension to split the big list into smaller sub lists with the size of 'sizeOfGroups'
for group in groups: #loop through each esub list
    outfile.write('({0}=={1}) and\n'.format(group[0].replace('\n',''), group[1].replace('\n',''))) #format and write to the outfile
outfile.close()
As it says in the comments of line 4, it removes the last element if the list is an odd number. You can always change it to this, so it adds a filler element:
lines = lines + ['-----']
In my opinion, this doesn't give you a very pretty output because it would be something like this:
Output:
(000123461==-----) and
Note: the list comprehension on line 5 is the same as this:
sub_lists = []
for n in range(0, len(lines), sizeOfGroups): #the extra parameter in range() tells it how much it increases by each time - for this it goes: 2, 4, 6 ....
    sub_lists.append(lines[n:n+sizeOfGroups]) #split the list into groups by taking the element 0-2 (2 not included so technically speaking 0-1) and so on.
Reply
#3
(Dec-21-2019, 06:53 PM)DreamingInsanity Wrote: You can do something like this:
lines = infile.readlines() #put all the lines in an array
sizeOfGroups = 2
if len(lines) % sizeOfGroups == 1: #if it's an odd number of elements
    lines.remove(lines[-1]) #Remove the last element - you could always add a new element to the end intead of removing.
groups = [lines[n:n+sizeOfGroups] for n in range(0, len(lines), sizeOfGroups)] #use a list comprehension to split the big list into smaller sub lists with the size of 'sizeOfGroups'
for group in groups: #loop through each esub list
    outfile.write('({0}=={1}) and\n'.format(group[0].replace('\n',''), group[1].replace('\n',''))) #format and write to the outfile
outfile.close()
As it says in the comments of line 4, it removes the last element if the list is an odd number. You can always change it to this, so it adds a filler element:
lines = lines + ['-----']
In my opinion, this doesn't give you a very pretty output because it would be something like this:
Output:
(000123461==-----) and
Note: the list comprehension on line 5 is the same as this:
sub_lists = []
for n in range(0, len(lines), sizeOfGroups): #the extra parameter in range() tells it how much it increases by each time - for this it goes: 2, 4, 6 ....
    sub_lists.append(lines[n:n+sizeOfGroups]) #split the list into groups by taking the element 0-2 (2 not included so technically speaking 0-1) and so on.

DreamingInsanity!
Thanks for your help and input - Merry Christmas!
// J
Reply
#4
Use code tag @jenost.
Here one way.
with open('regina.txt') as f,open('regina_id.txt', 'w') as f_out:
    l = [i.strip() for i in f]
    lst = zip(l[::2], l[1::2])
    for numb in lst:
        f_out.write(f'{numb[0]} == {numb[1]}\n')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problems writing a large text file in python Vilius 4 1,067 Dec-21-2024, 09:20 AM
Last Post: Pedroski55
  writing list to csv file problem jacksfrustration 5 2,445 Jul-04-2024, 08:15 PM
Last Post: deanhystad
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 3,825 Sep-27-2022, 01:38 PM
Last Post: buran
  Writing to json file ebolisa 1 1,729 Jul-17-2022, 04:51 PM
Last Post: deanhystad
  Writing to External File DaveG 9 4,540 Mar-30-2022, 06:25 AM
Last Post: bowlofred
  Writing to file ends incorrectly project_science 4 3,843 Jan-06-2021, 06:39 PM
Last Post: bowlofred
  Writing unit test results into a text file ateestructural 3 6,871 Nov-15-2020, 05:41 PM
Last Post: ateestructural
  Writing to file in a specific folder evapa8f 5 4,752 Nov-13-2020, 10:10 PM
Last Post: deanhystad
  Failure in writing binary text to file Gigux 7 6,055 Jul-04-2020, 08:41 AM
Last Post: Gigux
  writing data to a csv-file apollo 1 3,052 Jul-03-2020, 02:28 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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