【问题标题】:replacing every 4 word string in a text file [duplicate]替换文本文件中每 4 个单词的字符串 [重复]
【发布时间】:2019-12-05 11:08:26
【问题描述】:
def abc(filename):
    infile = open(filename, 'r+')
    read = infile.read()
    lst = read.split()

    for i in lst:
        if len(i) == 4:
            i.replace(i, 'abcd')
            print(i)

abc('question 4.txt')

我想替换我的文本文件中的 4 个字符串,但显然这没有发生。一切似乎都运行良好,但替换功能没有响应。

【问题讨论】:

标签: python python-3.x for-loop replace user-defined-functions


【解决方案1】:

这是一个可能的解决方案:

 for n, i in enumerate(lst):
    if len(i) == 4:
        lst[n] = 'abcd'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-05
    • 2013-04-05
    • 2014-08-24
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 2017-12-20
    相关资源
    最近更新 更多