当前位置: 首页 > 面试题库 >

Python expandtabs字符串操作

长孙兴德
2023-03-14
问题内容

我正在学习Python,并开始使用expandtabsPython编写命令。这是文档中的正式定义:

string.expandtabs(s[, tabsize])

将字符串中的制表符扩展为一个或多个空格,具体取决于当前列和给定的制表符大小。在字符串中出现每个换行符后,列号将重置为零。这不了解其他非打印字符或转义序列。选项卡的大小默认为8。

因此,据我了解,制表符的默认大小为8,要增加该大小,我们可以使用其他值

因此,当我在外壳中尝试时,我尝试了以下输入-

>>> str = "this is\tstring"
>>> print str.expandtabs(0)
this isstring
>>> print str.expandtabs(1)
this is string
>>> print str.expandtabs(2)
this is string
>>> print str.expandtabs(3)
this is  string
>>> print str.expandtabs(4)
this is string
>>> print str.expandtabs(5)
this is   string
>>> print str.expandtabs(6)
this is     string
>>> print str.expandtabs(7)
this is       string
>>> print str.expandtabs(8)
this is string
>>> print str.expandtabs(9)
this is  string
>>> print str.expandtabs(10)
this is   string
>>> print str.expandtabs(11)
this is    string

所以在这里,

  • 0 完全删除制表符,
  • 1与默认值完全相同8
  • 2完全像1然后
  • 3 是不同的
  • 然后4就像使用1

之后,它增加了,直到8这是默认设置,然后8.But后增加为什么怪异图案的数字从0到8?我知道应该从8开始,但是原因是什么呢?


问题答案:

str.expandtabs(n)不等同于str.replace("\t", " " * n)

str.expandtabs(n)跟踪每行上的当前光标位置,并用从当前光标位置到下一个制表位的空格数替换找到的每个制表符。制表位被视为每个n字符。

这是制表符工作方式的基础,而不是特定于Python。有关制表位的详细说明,请参见此问题的答案。

string.expandtabs(n) 等效于:

def expandtabs(string, n):
    result = ""
    pos = 0
    for char in string:
        if char == "\t":
            # instead of the tab character, append the
            # number of spaces to the next tab stop
            char = " " * (n - pos % n)
            pos = 0
        elif char == "\n":
            pos = 0
        else:
            pos += 1
        result += char
    return result

并举例说明:

>>> input = "123\t12345\t1234\t1\n12\t1234\t123\t1"
>>> print(expandtabs(input, 10))
123       12345     1234      1
12        1234      123       1

请注意,每个制表符("\t")如何被替换为使其与下一个制表符对齐的空格数量。在这种情况下,因为我提供了,所以每10个字符都有一个制表位n=10



 类似资料:
  • SETRANGE key offset value 用value 参数覆写(overwrite)给定key 所储存的字符串值,从偏移量offset 开始。 不存在的key 当作空白字符串处理。可以用作append: 注意: 如果偏移量>字符长度, 该字符自动补0x00,注意它不会报错

  • substr key start end 返回截取过的key的字符串值,注意并不修改key的值。下标是从0开始的

  • append key value 返回新字符串值的长度。

  • 前言 忙活了一个礼拜,终于等到周末,可以空下来写点东西。 之前已经完成《数值运算》和《布尔运算》,这次轮到介绍字符串操作 。咱们先得弄明白两个内容: 什么是字符串? 对字符串有哪些操作? 下面是"在线新华字典"的解释: 字符串:简称“串”。有限字符的序列。数据元素为字符的线性表,是一种数据的逻辑结构。在计算机中可有不同的存储结构。在串上可进行求子串、插入字符、删除字符、置换字符等运算。 而字符呢?

  • 字符串操作 函数 char *  rt_strstr (const char *s1, const char *s2)   判断字符串   rt_uint32_t  rt_strcasecmp (const char *a, const char *b)   忽略大小写比较字符串   char *  rt_strncpy (char *dst, const char *src, rt_ubase_

  • GETRANGE key start end 返回key 中字符串值的子字符串,字符串的截取范围由start 和end 两个偏移量决定(包括start 和end 在内)。可以使用负值,字符串右面下标是从-1开始的。 注意返回值处理: 1: start>=length, 则返回空字符串 2: stop>=length,则截取至字符结尾 3: 如果start 所处位置在stop右边, 返回空字符串

  • 注意:位操作中的位置是反过来的,offset过大,则会在中间填充0,比如 SETBIT bit 0 1,此时bit为10000000,此时再进行SETBIT bit 7 1,此时bit为10000001。offset最大2^32-1。 GETBIT key offset / SETBIT key offset value 设置某个索引的位为0/1 bitcount 对位进行统计 bitop 对1个

  • 问题 你想在字节字符串上执行普通的文本操作(比如移除,搜索和替换)。 解决方案 字节字符串同样也支持大部分和文本字符串一样的内置操作。比如: >>> data = b'Hello World' >>> data[0:5] b'Hello' >>> data.startswith(b'Hello') True >>> data.split() [b'Hello', b'World'] >>> dat