Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
wrapping problem
#1
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]))

Attached Files

.txt   ab010120.txt (Size: 114.56 KB / Downloads: 265)
Reply
#2
could you try this?
with open(filepath, 'r') as fp:
     
    data=fp.readlines()
Reply
#3
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Wrapping c/c++ dll Oolongtea 2 723 Aug-25-2023, 10:35 PM
Last Post: PyDan
  How to do "fixed size" (wrapping) math in Python? AlexanderWulf 13 1,852 Jul-19-2023, 04:13 PM
Last Post: deanhystad
  xml indent SubElements (wrapping) with multiple strings ctrldan 2 1,458 Jun-09-2023, 08:42 PM
Last Post: ctrldan
  wrapping c++ library JESuh 2 1,296 Jun-16-2022, 08:18 AM
Last Post: JESuh
  Help Wrapping C Library LeftyGuitar 2 1,840 Oct-06-2019, 08:04 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

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