我正在尝试编写一个函数,该函数查找字符串的最常见分隔符,它可以是逗号,空格或制表符。我的问题是该函数不读取选项卡,而是将它们读取为一堆空格。这是我的代码:
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 {
}
答案 0 :(得分:0)
如果您确定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.