Python Forum

Full Version: wrapping problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a text file which contain header as well as data section. I want to read the data section but when i wrap the data into 582 character and check the length of data i got different length.please help me to sort out this problem. I am attaching the file
from textwrap import wrap
filepath='/media/ibaad/IBAAD/ab010120.18n'
with open(filepath) as fp:
    
    data=fp.read() # read all the data into single string
    data1=data.split('END OF HEADER') # split a string into list
    
    
    data_section=data1[1]
    data_section=data_section[1:]
    data_section=wrap(data_section,582)
    print(len(data_section[0]))
    print(len(data_section[1]))
could you try this?
with open(filepath, 'r') as fp:
     
    data=fp.readlines()
The documentation says
Output:
Help on function wrap in textwrap: textwrap.wrap = wrap(text, width=70, **kwargs) Wrap a single paragraph of text, returning a list of wrapped lines. Reformat the single paragraph in 'text' so it fits in lines of no more than 'width' columns, and return a list of wrapped lines. By default, tabs in 'text' are expanded with string.expandtabs(), and all other whitespace characters (including newline) are converted to space. See TextWrapper class for available keyword args to customize wrapping behaviour.
The no more than 'width' columns could be the answer to your question. Textwrap.wrap() doesn't promise that every line has the same length.