Hey, I'm trying to figure out how to get this kind of program to work, it's supposed to be a UNIX style program in the sense that everything is command line driven. What I'm trying to get it to do is when the function is called it takes the input file and converts all tabs into 4 spaces. Here's the form of the input and what I have of my tab_to_space function so far.

"python3 assignment3.py +t < assignment3.py > output.txt"

Where +t denotes the function that is being called

Here is my function so far.

def tab_to_space():
    """ This function changes tabs in the indenting to spaces
    print("Tabbing spaces")
    for TAB_CHAR in input():
        string = input()
        string.replace("   ", "    ")
        return string

Recommended Answers

All 5 Replies

First off, you'll want to use the tab escape sequence, "\t", rather than the tab character itself in your string replace. Second, your are returning the function at the end of the first pass of the loop; you seem confused as to how you want the function to work, whether it should operate on the input directly or take a local argument. I would recommend re-writing it to take a string argument and return the modified string, then call it from a loop that does the actual input and output.

On the subject of input and output, if you are writing a filter, I would recommend a with statement on sys.stdin and a loop on readlines() rather than an explicit input() statement.

Oh you know what I called the input statement but I had in fact written my own input statement for the program that tests for EOF.

You might want to check module re

So I have it reading partially now, but it stops after reading a set of lines. The output stops at line 21. Can someone explain this, here's the whole program.

#Name: Nathan Geist
#ID #: *******
#Tut: 06

#Assignment 3
#Code Editing Program:

# 1) Change tabs in the indenting to spaces
# 2) Change spaces in the indenting to tabs
# 3) Substitute spaces, tabs, and newlines for printable characters
# 4) Undo 3
# *Functions will not be used at the same time*
# *Using command line qualifiers to allow user to specify functions to use*
# *Using a (-) sign to introduce a short qualifier*

import sys

#Defining Constants
EOF = chr(4) # Standard End of File character
TAB_CHAR = chr(187) # ">>" character used to make tab character visible
SPACE_CHAR = chr(183) # Raised dot used to make space character visible
NEWLINE_CHAR = chr(182) # Backwards p used to make newline character visible


def get_input():
    """This function works like input() (with no arguments) except that
    instead of throwing an exception when encountering EOF it will return an
    EOF character (char(4))
    Returns: a line of input or EOF if end of file"""

        ret = input()
    except EOFError:
        ret = ret+EOF
    return ret

#Tabs to spaces
def tab_to_space(file_in):
    """ This function changes tabs in the indenting to spaces
    print("Tabbing spaces")
    output_string = ""

    for line in file_in:
        input_string = get_input()
        input_string = input_string.replace("/t", "    ")
        output_string = output_string+input_string

    return output_string

def help():
    """Prints out the help file for the program, telling the user how to
    use certain functions as well as the limitations of the arguments"""

\nThis program processes python files in the following ways:\n\
1) Changes tabs in the indenting to spaces\n\
2) Changes spaces in the indenting to tabs\n\
3) Substitute spaces, tabs, and newlines for their printable characters\n\
4) Undo the actions of 3\n\
+t (Replaces sequences of spaces of length T with a single tab)\n\
-t (Replaces tabs with sequences of T spaces\n\
-T<integer> (Specifies the space-to-tab ration, T. Default = 4)\n\
+v (Changes all spaces, tabs, and newlines to visible characters\n\
-v (Undoes the actions of +v)\n\
-help (Prints out this help text)\n\
+t and -t are incompatible\n\
+v and -v are incompatible\n\n\
< (Defines the input program)\n\
> (Defines the output file)\n\
Enter in the following form:\n\
python3 program.py +v +t -T4 < input.py > temp.out")

#End of help function

def space_to_tab():

def main():
    file_in = get_input()
    print (file_in)
    first_arg = True
    for arg in sys.argv:
        if first_arg: #The first argument = program name
            first_arg = False

        elif (arg == "-t"):

        elif (arg == "+t"):

        elif (arg == "+v"):

        elif (arg == "-v"):

        elif (arg == "-help"):

            print("Unrecognized argument")

#End of main function


This program may help

# -*-coding: utf8-*-

__doc__ = '''

import sys

S4 = " " * 4

def main():
    while True:
        line = sys.stdin.readline()
        if not line:
        result = line.replace('\t', S4)
        print(result, end='')

if __name__ == '__main__':
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.