Python Forum
Split Characters As Lines in File
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Split Characters As Lines in File
#1
Hello,
I have this txt file. (The length is 15000)
1010100101010110000000000001111111111
And according to input, I want to separate my numbers as different lines and each line will have equal size
For instance, suppose that i is the input and i =10
then my text file will become like that:
1010100101
0101011000
0000000000...

The code should be something like that
with open("names.txt","r") as f:
    input_list = f.read()    

def write_len(input_list):
    final_str = "\n".join([str(len(item)) for item in input_list])

    with open("name_length.txt","w") as w:
        w.write(final_str)

    return 1
But I couldn't complete it
Can you please help me?
Best
Reply
#2
You could loop over every character in the string, assembling them into strings of the proper length.

Or you could slice it into sections of the proper length.

k = 10
sections = [input_list[i:i+k] for i in range(0, len(input_list), k)]
Then you can join() the sections or print them or whatever you want with them.
Reply
#3
There is built-in module textwrap:

>>> import textwrap
>>> s = '1010100101010110000000000001111111111'
>>> print('\n'.join(textwrap.wrap(s, width=10)))
1010100101
0101100000
0000000111
1111111
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
(Dec-28-2020, 04:48 AM)perfringo Wrote: There is built-in module textwrap:

>>> import textwrap
>>> s = '1010100101010110000000000001111111111'
>>> print('\n'.join(textwrap.wrap(s, width=10)))
1010100101
0101100000
0000000111
1111111

Many thank to both of you
here is my final code
import itertools
import random
from collections import defaultdict
import pprint
import fileinput
import textwrap

with open("line500_.txt","r") as f:
    input_list = f.read()    
 
k = 10
#sections = [input_list[i:i+k] for i in range(0, len(input_list), k)]
#final_str = "\n".join([str(len(item)) for item in input_list])
#final = "\n".join(str(sections))
 
twrap =  '\n'.join(textwrap.wrap(input_list, width=10))
print(twrap)   

myfile =  open("name_length.txt","w")
with open("name_length.txt","w") as w:
    w.write(str(twrap))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  doing string split with 2 or more split characters Skaperen 22 2,317 Aug-13-2023, 01:57 AM
Last Post: Skaperen
  How to "tee" (=split) output to screen and into file? pstein 6 1,289 Jun-24-2023, 08:00 AM
Last Post: Gribouillis
  Split pdf in pypdf based upon file regex standenman 1 1,974 Feb-03-2023, 12:01 PM
Last Post: SpongeB0B
  counting lines in split data Skaperen 6 1,348 Oct-07-2022, 07:09 PM
Last Post: Skaperen
  Delete multiple lines from txt file Lky 6 2,202 Jul-10-2022, 12:09 PM
Last Post: jefsummers
  failing to print not matched lines from second file tester_V 14 5,945 Apr-05-2022, 11:56 AM
Last Post: codinglearner
  Extracting Specific Lines from text file based on content. jokerfmj 8 2,857 Mar-28-2022, 03:38 PM
Last Post: snippsat
  How to split file by same values from column from imported CSV file? Paqqno 5 2,705 Mar-24-2022, 05:25 PM
Last Post: Paqqno
  [split] Results of this program in an excel file eisamabodian 1 1,543 Feb-11-2022, 03:18 PM
Last Post: snippsat
  split txt file data on the first column value shantanu97 2 2,377 Dec-29-2021, 05:03 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