当前位置:网站首页>Why should I use the source code of nametuple replace(‘,‘, ‘ ‘). Split() instead of split(‘,‘)

Why should I use the source code of nametuple replace(‘,‘, ‘ ‘). Split() instead of split(‘,‘)

2022-06-21 06:19:00 Channing Lewis

 Insert picture description here
Recently in to see Python Of collections Bag nametuple Source code , See two lines of string manipulation :

if isinstance(field_names, str):
    field_names = field_names.replace(',', ' ').split()

I'm curious , If you want to separate strings with commas , direct writing :

field_names = field_names.split(',')

Just fine .

Later, I realized that it was to be compatible with the situation of space segmentation . The source code can be written either by comma or by space , And it can be mixed ; But my writing only supports comma division .

原网站

版权声明
本文为[Channing Lewis]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206210600417328.html