简体   繁体   中英

How do I differentiate between a tab and a space in a string in python

I'm trying to write a function that finds the most common delimiter of a string, it's either a comma, a space or a tab. My issue is that the function doesn't read tabs and instead reads them as a bunch of spaces. Here's the code I have:

def which_delimiter(input_str):

    # count total number of spaces, commas and tabs in the string
    spaces = input_str.count(" ")
    commas = input_str.count(",")
    tabs = input_str.count("\t")

    # return the delimiter with the highest frequency
    # if there is none, raise an error
    if spaces == 0 and commas == 0 and tabs == 0:
        raise AssertionError
    elif spaces > commas and spaces > tabs:
        return "spaces"
    elif commas > spaces and commas > tabs:
        return "commas"
    else:
        return "tabs"

If you are sure your input_str contains \\t char, then your function is correct.

But if you char contains a tab, it will be interpreted as 4 spaces characters. A workaround for you is to count the bunch of spaces using input_str.count(" ")

Which gives :

def which_delimiter(input_str):

    # count total number of spaces, commas and tabs in the string
    spaces = input_str.count(" ")
    commas = input_str.count(",")
    tabs = input_str.count("\t")
    bunch_spaces = input_str.count("    ")

    # You can count tabs and bunch spaces separately or regroup them, using for example
    # tabs_total_count = tabs + bunch_spaces.
    # Then you use your if/elif statements as you want with above elements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM