Python 切片字符串或添加到列表列表中的字符串

标签 python string list

我正在尝试根据元素的长度更改列表列表中的元素,如果字符串长度大于 8,我想将字符串从开头截断到长度 8,如果字符串短于 8,我想想要添加多少它在空白处更短。这段代码哪里出错了?

list_of_lists = [['PARENT', 'ID', 'DESCRIPTION1', 'DESCRIPTION2'],
             ['', 'R1', 'R1_Des_1', '0'],
             ['C1', 'E1', 'E1_Des_1', '1'],
             ['P1', 'C1', 'C1_Des_1', '2'],
             ['R1', 'P1', 'P1_Des_1', '3'],
             ['C2', 'E2', 'E2_Des_1', '4'],
             ['P2', 'C2', 'C2_Des_1', '5'],
             ['R1', 'P2', 'P2_Des_1', '6'],
             ['P2', 'C3', 'C3_Des_1', '7'],
             ['C4', 'E3', 'E3_Des_1', '8'],
             ['P3', 'C4', 'C4_Des_1', '9'],
             ['R2', 'P3', 'P3_Des_1', '10'],
             ['None', 'R3', 'R3_Des_', '11'],
             ['P3', 'C5', 'C5_Des_1', '12'],
             ['C6', 'E4', 'E4_Des_1', '13'],
             ['P4', 'C6', 'C6_Des_1', '14'],
             ['R2', 'P4', 'P4_Des_1', '15'],
             ['C7', 'E5', 'E5_Des_1', '16'],
             ['P5', 'C7', 'C7_Des_1', '17'],
             ['R3', 'P5', 'P5_Des_1', '18'],
             ['C9', 'E6', 'E6_Des_1', '19'],
             ['P6', 'C9', 'C9_Des_1', '20'],
             ['R3', 'P6', 'P6_Des_1', '21'],
             ['P6', 'C8', 'C8_Des_1', '22'],
             ['C10', 'E7', 'E7_Des_1', '23'],
             ['P7', 'C10', 'C10_Des_1', '24'],
             ['R4', 'P7', 'P7_Des_1', '25'],
             ['P7', 'C11', 'C11_Des_1', '26'],
             ['C12', 'E8', 'E8_Des_1', '27'],
             ['P8', 'C12', 'C12_Des_1', '28'],
             ['R4', 'P8','','29'],
             ['', 'P8','','30'],
             ['R1', 'P8','','31'],
             ['', 'z1','','32'],
             ['z1', 'z2','','33'],
             ['z1', 'z3','','34'],
             ['z2', 'z4','','35'],
             ['z3', 'z4','','36']]

list_of_lists_slice = list(list_of_lists)
list_string = []

for sublist in list_of_lists_slice:
    for element in sublist:
        if len(element) > 8:
            element = element[:7]
        if len(element) < 8:
            space = 8 - len(element)
            element = element + (' ' * space)


for sublist in list_of_lists_slice:
    row_str = str(' |')
    ele_str = '|'.join(sublist)
    row_str = row_str + ele_str + ('| ')
    list_string.append(row_str)
    middle = ('  ')
    middle = middle + str((len(sublist)) * (9 * ('-')))
    list_string.append(middle)

for row in list_string:
    print (row)

最佳答案

我不知道你的代码有什么问题,但你绝对应该使用字符串格式,结合格式语言的截断填充选项:

>>> s1 = 'longer than 8'
>>> s2 = 'short'
>>> f'{s1:8.8}'
'longer t'
>>> f'{s2:8.8}'
'short   '

如果你得到这些 f-strings 的 SyntaxError,使用老派的方法:

>>> '{:8.8}'.format(s1)
'longer t'
>>> '{:8.8}'.format(s2)
'short   '

关于Python 切片字符串或添加到列表列表中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43214474/

相关文章:

java - 从字符串数组 java 中的标记/单词构建句子

c# - 排序字母数字字符串,然后交替数字/字母字符输出

java - CompareTo() 在客户端/服务器应用程序中给出相互冲突的结果?

Python,在列表列表中,将第一个列表的第一个元素与第二个列表的第一个元素进行比较

android - 如何将多个对象放入 android 的 ScrollView 条目中?

python - 将包含python列表的文本文档读入python程序

python - 在 python 中运行 shell 命令并读取输出

python - 无法更新标签文本

python pandas删除重复的列

python - python中itertools.izip的反函数是什么?