textwrap.dedent替换标签? [英] textwrap.dedent replaces tabs?

查看:91
本文介绍了textwrap.dedent替换标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

dedent的文档说,从'text`的每一行的左边统一删除任何空格而不是

,但我是

发现它还在修改''\t''字符,这在我的申请中是非常不可取的。有没有什么方法可以阻止它做这个,或者将这些标签放回去?


我看到TextWrap及其成员函数采用一个''expand_tabs''

kwarg,但不幸的是没有。


....我想这样的功能(替代dedent)不是'''非常强硬

写,但它似乎是一个奇怪的默认,特别是考虑到没有

方式来关闭不希望的变化,但是变化/不是/制作,

相同的文字可以通过TextWrapper传递并让它们移除
......


thx,

-tom!


-

解决方案

嗯...快点修复可能是暂时用另一个相对未使用的控制字符替换所有制表符




MyString = MyString.replace(" \t", chr(1))

MyString = textwrap.dedent(MyString)

MyString = MyString.replace(chr(1)," \t")


当然......这不是完全安全的,但它不是将致命,

如果它确实搞砸了。只要你不希望收到任何

ASCII 1个字符。


CakeProphet写道:
< blockquote class =post_quotes>
嗯......一个快速修复可能是暂时用另一个相对未使用的控制角色替换所有制表符




MyString = MyString.replace(" \t",chr(1))

MyString = textwrap.dedent(MyString)

MyString = MyString。替换(chr(1)," \t")


当然......这不是完全安全的,但它不会是致命的,

如果确实搞砸了。只要你不希望收到任何

ASCII 1个字符。



好​​吧,还有一个小问题,即我想要剥离的前导标签。我想我可以用8个

空格手动替换所有标签(而不是''正确''制表位),然后在完成

时替换它们,但它'这可能就像写一个非破坏性的dedent一样容易。


这不是我不明白/为什么/它做到了;确实,我确定它是这样做的,所以你可以在Python源代码中混合制表符和空格。为什么有人

会故意这样做,但是,我不确定。 ;)


-tom!


-


Tom Plunket写道:


I * guess *我*可以*手动*替换* all * tabs * with * eig ht

space(而不是' '正确''制表符停止),然后在完成

时替换它们,但编写一个非破坏性的dedent可能同样容易。



你的意思是,就像


>>" \talpha\tbeta\t" .expandtabs()



'' alpha beta''





彼得


The documentation for dedent says, "Remove any whitespace than can be
uniformly removed from the left of every line in `text`", yet I''m
finding that it''s also modifying the ''\t'' characters, which is highly
undesirable in my application. Is there any way to stop it from doing
this, 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 tough
to write, but it seems an odd default especially considering there''s no
way to turn off the undesired changes, but were the changes /not/ made,
the same text could just be passed through TextWrapper and have them
removed...

thx,
-tom!

--

解决方案

Hmmm... a quick fix might be to temporarily replace all tab characters
with another, relatively unused control character.

MyString = MyString.replace("\t", chr(1))
MyString = textwrap.dedent(MyString)
MyString = MyString.replace(chr(1), "\t")

Of course... this isn''t exactly safe, but it''s not going to be fatal,
if it does mess something up. As long as you don''t expect receiving any
ASCII 1 characters.


CakeProphet wrote:

Hmmm... a quick fix might be to temporarily replace all tab characters
with another, relatively unused control character.

MyString = MyString.replace("\t", chr(1))
MyString = textwrap.dedent(MyString)
MyString = MyString.replace(chr(1), "\t")

Of course... this isn''t exactly safe, but it''s not going to be fatal,
if it does mess something up. As long as you don''t expect receiving any
ASCII 1 characters.

Well, there is that small problem that there are leading tabs that I
want stripped. I guess I could manually replace all tabs with eight
spaces (as opposed to ''correct'' tab stops), and then replace them when
done, 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 it
does this so you can mix tabs and spaces in Python source. Why anyone
would intentionally do that, though, I''m not sure. ;)

-tom!

--


Tom Plunket wrote:

I*guess*I*could*manually*replace*all*tabs*with*eig ht
spaces (as opposed to ''correct'' tab stops), and then replace them when
done, but it''s probably just as easy to write a non-destructive dedent.

You mean, as easy as

>>"\talpha\tbeta\t".expandtabs()

'' alpha beta ''

?

Peter


这篇关于textwrap.dedent替换标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆