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
I have this txt file. (The length is 15000)
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:

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

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:

    return 1
But I couldn't complete it
Can you please help me?
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.
There is built-in module textwrap:

>>> import textwrap
>>> s = '1010100101010110000000000001111111111'
>>> print('\n'.join(textwrap.wrap(s, width=10)))
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.
(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)))

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 =    
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))

myfile =  open("name_length.txt","w")
with open("name_length.txt","w") as w:

Possibly Related Threads…
Thread Author Replies Views Last Post
  Importing a function from another file runs the old lines also dedesssse 6 350 Jul-06-2021, 07:04 PM
Last Post: deanhystad
  [split] Help- converting file with pyton script eltomassito 6 634 Jul-02-2021, 05:29 PM
Last Post: snippsat
  [Solved] Trying to read specific lines from a file Laplace12 7 726 Jun-21-2021, 11:15 AM
Last Post: Laplace12
  all i want to do is count the lines in each file Skaperen 13 1,118 May-23-2021, 11:24 PM
Last Post: Skaperen
  failing to print not matched lines from second file tester_V 13 1,465 Apr-13-2021, 03:36 PM
Last Post: DeaD_EyE
  [split] How to convert the CSV text file into a txt file Pinto94 5 868 Dec-23-2020, 08:04 AM
Last Post: ndc85430
  Find lines from one file in another tester_V 8 1,042 Nov-15-2020, 03:29 AM
Last Post: tester_V
  get two characters, count and print from a .txt file Pleiades 9 1,083 Oct-05-2020, 09:22 AM
Last Post: perfringo
  How to split and combine embedded lines using less code pjfarley3 6 830 Aug-13-2020, 09:13 PM
Last Post: pjfarley3
  Iterate 2 large text files across lines and replace lines in second file medatib531 13 1,780 Aug-10-2020, 11:01 PM
Last Post: medatib531

Forum Jump:

User Panel Messages

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