0

我试图逐行读取文件,对于我读取的每一行,我尝试将其拆分,然后我想将它写在我得到它的地方。为了做到这一点,我写了这样一个代码:

fst_file=open(fst_text,"r+")
    line=fst_file.readline()

    while line:
        temp=(line.split('CONFIG_',1)[1]).split('PATH=')
        temp[1]=temp[1].replace('\n','')
        fst_file.write(temp)        
        line=fst_file.readline()

    fst_file.close()

但是我收到了如下错误:

File "test.py", line 84, in <module>
    branchName()
  File "test.py", line 75, in branchName
    fst_file.write(temp)        
TypeError: expected a character buffer object

有人帮我吗?

4

1 回答 1

1

因为您已经.split在线使用,所以您正在使用字符串列表。虽然字符串在概念上只是一个字符列表,但它仍然不是字符串列表。

在写出之前尝试使用字符串的 .join 方法加入数组,例如:

''.join(["one, two"]) == "onetwo"

于 2013-08-22T08:24:28.817 回答