Python 为什么这种替换方法不能正常工作

Python 为什么这种替换方法不能正常工作,python,string,replace,Python,String,Replace,我有一个包含如下数据的文件: 1 1:-0.03125 2:0.236364 3:0.142857 4:-0.107438 5:0.129032 6:0.163636 7:0.242105 2 1:-0.03125 2:0.472727 3:0.761905 4:0.123967 6:0.472727 7:0.347368 .... with open("file1.test", "r") as infile: text = [i.strip() for i in infile.rea

我有一个包含如下数据的文件:

1 1:-0.03125 2:0.236364 3:0.142857 4:-0.107438 5:0.129032 6:0.163636 7:0.242105
2 1:-0.03125 2:0.472727 3:0.761905 4:0.123967 6:0.472727 7:0.347368
....
with open("file1.test", "r") as infile:
    text = [i.strip() for i in infile.readlines()]

output = []
for t in text :
    output.append( ','.join( [i.split(':')[-1] for i in t.split()] ) )

with open("file1.csv", "w") as outfile:
    outfile.write( '\n'.join( output ) )
总共有几千行。我正在尝试将其转换为csv文件

我尝试了以下代码:

with open("file1.test", "r") as infile:
    with open("file1.csv", "w") as outfile:
        for line in infile:
            new_line = line
            i = 1
            for i in range(1,8):
                to_replace = " " + str(i) + ":"
                new_line = new_line.replace(to_replace, ",", 1)
            new_line = line[:2] + "," + line[2:]
            new_line = new_line.replace(" ", "", 1)


但它并没有像预期的那样起作用。行
new\u line=new\u line.replace(“,”,1)
起作用,但当我试图用逗号替换“1:”时,它不会更新。我希望我的最终文件看起来像“


你知道我做错了什么吗?

你可以对每一行尝试这种转换方法:

>>> a = '1 1:-0.03125 2:0.236364 3:0.142857 4:-0.107438 5:0.129032 6:0.163636 7:0.242105'
>>> ','.join( [i.split(':')[-1] for i in a.split()] )
'1,-0.03125,0.236364,0.142857,-0.107438,0.129032,0.163636,0.242105'
您的整个程序如下所示:

1 1:-0.03125 2:0.236364 3:0.142857 4:-0.107438 5:0.129032 6:0.163636 7:0.242105
2 1:-0.03125 2:0.472727 3:0.761905 4:0.123967 6:0.472727 7:0.347368
....
with open("file1.test", "r") as infile:
    text = [i.strip() for i in infile.readlines()]

output = []
for t in text :
    output.append( ','.join( [i.split(':')[-1] for i in t.split()] ) )

with open("file1.csv", "w") as outfile:
    outfile.write( '\n'.join( output ) )

您可以对每一行尝试这种转换方法:

>>> a = '1 1:-0.03125 2:0.236364 3:0.142857 4:-0.107438 5:0.129032 6:0.163636 7:0.242105'
>>> ','.join( [i.split(':')[-1] for i in a.split()] )
'1,-0.03125,0.236364,0.142857,-0.107438,0.129032,0.163636,0.242105'
您的整个程序如下所示:

1 1:-0.03125 2:0.236364 3:0.142857 4:-0.107438 5:0.129032 6:0.163636 7:0.242105
2 1:-0.03125 2:0.472727 3:0.761905 4:0.123967 6:0.472727 7:0.347368
....
with open("file1.test", "r") as infile:
    text = [i.strip() for i in infile.readlines()]

output = []
for t in text :
    output.append( ','.join( [i.split(':')[-1] for i in t.split()] ) )

with open("file1.csv", "w") as outfile:
    outfile.write( '\n'.join( output ) )
我喜欢这个

import re  
with open("file1.test", "r") as infile:
    with open("file1.csv", "w") as outfile:
        for line in infile:
            outfile.write(re.sub(' [1-7]:',',',line))
我喜欢这个

import re  
with open("file1.test", "r") as infile:
    with open("file1.csv", "w") as outfile:
        for line in infile:
            outfile.write(re.sub(' [1-7]:',',',line))
您的
.replace()
s可能都工作得很好。但是,您放弃了所有的工作,只使用了
line
的原始值的语句
new\u line=line[:2]+“,“+line[2://code>
s可能都工作得很好。但是,您将所有这些工作与语句
new_line=line[:2]+“,“+line[2:][/code>”一起丢弃,该语句仅使用
line
的原始值。