本文介绍了textwrap.dedent替换标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 dedent的文档说,从'text`的每一行的左边统一删除任何空格而不是 ,但我是 发现它还在修改''\t''字符,这在我的申请中是非常不可取的。有没有什么方法可以阻止它做这个,或者将这些标签放回去? 我看到TextWrap及其成员函数采用一个''expand_tabs'' kwarg,但不幸的是没有。 ....我想这样的功能(替代dedent)不是'''非常强硬 写,但它似乎是一个奇怪的默认,特别是考虑到没有 方式来关闭不希望的变化,但是变化/不是/制作, 相同的文字可以通过TextWrapper传递并让它们移除 ...... thx, -tom! - 解决方案 好​​吧,还有一个小问题,即我想要剥离的前导标签。我想我可以用8个 空格手动替换所有标签(而不是''正确''制表位),然后在完成 时替换它们,但它'这可能就像写一个非破坏性的dedent一样容易。 这不是我不明白/为什么/它做到了;确实,我确定它是这样做的,所以你可以在Python源代码中混合制表符和空格。为什么有人 会故意这样做,但是,我不确定。 ;) -tom! - 你的意思是,就像 '' alpha beta'' ? 彼得 The documentation for dedent says, "Remove any whitespace than can beuniformly removed from the left of every line in `text`", yet I''mfinding that it''s also modifying the ''\t'' characters, which is highlyundesirable in my application. Is there any way to stop it from doingthis, or alternatively, to put those tabs back in? I see that TextWrap and its member functions take an ''expand_tabs''kwarg, but dedent unfortunately does not. ....I suppose such a function (replacement dedent) isn''t terribly toughto write, but it seems an odd default especially considering there''s noway to turn off the undesired changes, but were the changes /not/ made,the same text could just be passed through TextWrapper and have themremoved... thx,-tom! -- 解决方案 Well, there is that small problem that there are leading tabs that Iwant stripped. I guess I could manually replace all tabs with eightspaces (as opposed to ''correct'' tab stops), and then replace them whendone, but it''s probably just as easy to write a non-destructive dedent. It''s not that I don''t understand /why/ it does it; indeed I''m sure itdoes this so you can mix tabs and spaces in Python source. Why anyonewould intentionally do that, though, I''m not sure. ;) -tom! --You mean, as easy as '' alpha beta '' ? Peter 这篇关于textwrap.dedent替换标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-14 15:32