Python Forum
removing tabs from input text
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
removing tabs from input text
#1
Hi, was wondering if you could help. My file f contains some tabs and I want to remove them. I have a code below that does read text line by line but does not remove tabs.
I have something like:
0 0 1 1
1 1 0 0
I want:
0011
1100
Thank you!

def readFromFile(f):

    input_j = list()

    with open(f) as file:

        for line in file:

            input_j.append(list(line.rstrip()))
Reply
#2
text = text.replace('\t', '')
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
In CPython desired result can be achieved by following way (gc closes file automagically); returns list of rows where whitespaces are removed:

def read_from_file(f):
    return [''.join(row.rstrip().split()) for row in open(f)] 
Same thing using with:

def read_from_file(f):
    with open(f, 'r', encoding='UTF-8') as obj:
         return [''.join(row.rstrip().split()) for row in obj]
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
If you do the same on module level, the fd is only closed, when the program ends.
I guess the fd is closed, because it's in the scope of the list comprehension.
After the list comprehension is done, the fd is not accessible from anywhere.
This means it will be garbage collected an hopefully closed.

I like context managers. If you use pathlib.Path('some_file').read_text() a contextmanager in the read_text method is used internally.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Sad How to split a String from Text Input into 40 char chunks? lastyle 7 1,120 Aug-01-2023, 09:36 AM
Last Post: Pedroski55
  Buttons not working in tabs kenwatts275 2 1,351 May-02-2022, 04:45 PM
Last Post: kenwatts275
  PYQT charts in tabs frohr 10 4,354 Feb-13-2022, 04:31 PM
Last Post: Axel_Erfurt
  create new tabs and populate using python reggie4 2 2,249 Jan-23-2021, 11:25 PM
Last Post: Larz60+
  Modify Input() text catosp 6 2,868 Jun-08-2020, 10:48 PM
Last Post: deanhystad
  Selecting text as an input lazerwolf101 2 2,229 May-29-2020, 06:52 AM
Last Post: lazerwolf101
  HELP TabError: inconsistent use of tabs and spaces in indentation blackjesus24 2 3,534 Jan-30-2020, 10:25 AM
Last Post: blackjesus24
  Setting Tabs in a listbox scratchmyhead 2 2,795 Nov-12-2019, 11:18 PM
Last Post: scratchmyhead
  removing spaces/tabs after used .strip() zarize 0 1,576 Sep-11-2019, 12:46 PM
Last Post: zarize
  Python QGIS tool that replaces layout text labels with attributes from an input table geodenn92 1 2,658 Aug-13-2019, 06:05 AM
Last Post: buran

Forum Jump:

User Panel Messages

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