如何在python中区分制表符和字符串中的空格

我正在尝试编写一个函数,该函数查找字符串的最常见分隔符,它可以是逗号,空格或制表符。我的问题是该函数不读取选项卡,而是将它们读取为一堆空格。这是我的代码:

import {Commonmodule} from '@angular/common';
import {NgModule} from '@angular/core';
import {FormsModule} from '@angular/forms';
import {EffectsModule} from '@ngrx/effects';
import {StoreModule} from '@ngrx/store';
import {TranslateModule} from '@ngx-translate/core';
import {AdjustmentsStepRouteGuard} from './steps/adjustments-step/adjustments-step.guard';
import {ArchitectureStepRouteGuard} from './steps/architecture-step/architecture-step.guard';
import {ProjectNameStepRouteGuard} from './steps/project-name-step/project-name-step.guard';
import {ResultsStepRouteGuard} from './steps/results-step/results-step.guard';
import * as fromWizardStore from './store/wizard';
import * as fromTSEProjectStore from './store/TSEProject';
import {TSEProjectsEffects} from './store/TSEProject/effects/TSEProject.effects';
import {WizardStepsComponent} from './wizard-steps/wizard-steps.component';
import {WizardComponent} from './wizard.component';
import {WizardRoutingModule} from './wizard.routing.module';
import {WizardNavigationmodule} from './wizard-navigation/wizard-navigation.module';

@NgModule({
  imports: [
    Commonmodule,FormsModule,WizardRoutingModule,StoreModule.forFeature('wizard',fromWizardStore.reducer),StoreModule.forFeature('TSEProject',fromTSEProjectStore.reducer),EffectsModule.forFeature([TSEProjectsEffects]),TranslateModule,WizardNavigationmodule
  ],declarations: [WizardComponent,WizardStepsComponent],providers: [ProjectNameStepRouteGuard,ArchitectureStepRouteGuard,AdjustmentsStepRouteGuard,ResultsStepRouteGuard]
})
export class Wizardmodule {
}
whtorry 回答:如何在python中区分制表符和字符串中的空格

如果您确定input_str包含\t字符,则您的函数正确。

但是,如果您的char包含一个制表符,它将被解释为4个空格字符。 一种解决方法是使用input_str.count(" ")

计算一堆空格

哪个给:

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.
本文链接:https://www.f2er.com/3008799.html

大家都在问