从每行中删除7个字符

2024-09-19 23:45:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我该怎么做?这段代码从每行中删除最后7个字符

string = """
    "bleach": 1.0,
    "ducttape": 1.0,
"""
result = string[:-7]
print(result)

Tags: 代码stringresultprintbleach个字符ducttape
2条回答

enter image description here

请在上面的图片中找到正确格式的代码

在这里,我们使用split方法在每行末尾拆分原始字符串,结果字符串列表存储在名为split的变量中。名为split的列表的长度存储在名为l的变量中。迭代列表中的每一行,并在列表的每个位置存储字符串(最后7个元素除外),方法是在一个新变量中使用字符串切片,并在每次迭代后将其与t连接

string = """
    "bleach": 1.0,
    "ducttape": 1.0,
"""
for row in string.split('\n'):
    result = row[:-6]
    result=result+","
    print(result)

相关问题 更多 >