TypeError: write() argument must be str, not list while writing to file

TypeError: write() argument must be str, not list while writing to file

我写了一个程序来帮助增加文件中的数字。 如果我使用 writelines 并且对于 f.write 我得到
则只能复制 第一个 行 f.write(new_line ) if lines[0].strip().endswith(':') else f.write([new_line, *lines]) TypeError: write() 参数必须是 str,而不是 list

file.txt

Django 2.1:0
    djangoAPI1
    djangoAPI2

我的代码

import re
regex = r'(?<=:)\d*$'
def incr_patch_version(fname):
    with open(fname, 'r+') as f:
        lines = f.readlines()
        #print  (lines[0])
        new_line = re.sub(regex, lambda x: str(int(x.group()) + 1 if x.group().isnumeric() else 0), lines[0])
        f.seek(0)
        print (*lines)
        f.write(new_line ) if lines[0].strip().endswith(':') else f.write([new_line, *lines])
        #f.write(new_line ) if lines[0].strip().endswith(':') else f.writelines([new_line, *lines])
fname = 'file.txt'
incr_patch_version(fname)

如果我使用 f.write(new_line ) if lines[0].strip().endswith(':') else f.writelines([new_line, *行]).我得到以下输出

Django 2.1:3
Django 2.1:2
Django 2.1:1
Django 2.1:0
    djangoAPI1
    djangoAPI2

2 次执行后所需的输出文件

Django 2.1:2
    djangoAPI1
    djangoAPI2        
Django 2.1:1
    djangoAPI1
    djangoAPI2        
Django 2.1:0
    djangoAPI1
    djangoAPI2

您的实际问题在这里:

else f.write([new_line, *lines])

如您所见,您将列表作为 write 方法的输入。

您可以像下面这样更新您的代码

import re
import os

regex = r'(?<=:)\d*$'


def incr_patch_version(fname):
    with open(fname, 'r+') as f:
        lines = f.readlines()

        i = 1

        while i < len(lines)and ":" not in lines[i]:
            i += 1

        new_line = re.sub(regex, lambda x: str(int(x.group()) + 1 if x.group().isnumeric() else 0), lines[0])

        if not lines[i-1].endswith(os.linesep):
            lines[i-1] += os.linesep

        f.seek(0)
        new_lines = [new_line, *lines[1:i], *lines]
        f.writelines(new_lines)
        print(*new_lines)


fname = 'file.txt'
incr_patch_version(fname)

文件中的输出如下所示

Django 2.1:4
    djangoAPI1
    djangoAPI2
Django 2.1:3
    djangoAPI1
    djangoAPI2
Django 2.1:2
    djangoAPI1
    djangoAPI2
Django 2.1:1
    djangoAPI1
    djangoAPI2
Django 2.1:0
    djangoAPI1
    djangoAPI2

这是一种方法。

import re
first_chunk = False
chunk = ""
with open(filename) as infile:
    content = infile.read()              #Read Content
    for line in content.splitlines():    #Iterate Lines
        match = re.match("\w+", line)    #Check for lable
        if match and first_chunk:        #Break if lable chunk found
            break
        elif match:
            first_chunk = True
            pattern = re.compile(r":(\d+)")
            chunk += pattern.sub(":{}".format(int(pattern.search(line).group(1))+ 1) ,  line)    #Increment count
        else:
            chunk += "\n" + line

with open(filename, "w") as outfile:            #Write back to file
    result = "{}\n{}".format(chunk, content)
    outfile.write(result)

输出:

Django 2.1:3
    djangoAPI1
    djangoAPI2
Django 2.1:2
    djangoAPI1
    djangoAPI2
Django 2.1:1
    djangoAPI1
    djangoAPI2
Django 2.1:0
    djangoAPI1
    djangoAPI2