Python 用逗号替换每行中的多个空格

Python 用逗号替换每行中的多个空格,python,csv,Python,Csv,如何用逗号替换每行的多个空格,我正在使用tablate,并一直在试图找出如何使用逗号 这里有一个我的代码: def print_extensions(self): i = InternetExplorer(self.os) content1 = tabulate(i.extensions(), headers="keys", tablefmt="plain") text_file=open("output.csv","w")

如何用逗号替换每行的多个空格,我正在使用tablate,并一直在试图找出如何使用逗号

这里有一个我的代码:

def print_extensions(self):       
    i = InternetExplorer(self.os)
    content1 = tabulate(i.extensions(), headers="keys", tablefmt="plain")               
    text_file=open("output.csv","w")
    text_file.write(content1)
    text_file.close()
path,name,id
C:\Windows,Microsoft,{CFBFAE00}
我的CSV输出:

其中•=空格

path•••••name•••••id
C:\Windows•••••Microsoft•••••{CFBFAE00}
预期CSV输出:

def print_extensions(self):       
    i = InternetExplorer(self.os)
    content1 = tabulate(i.extensions(), headers="keys", tablefmt="plain")               
    text_file=open("output.csv","w")
    text_file.write(content1)
    text_file.close()
path,name,id
C:\Windows,Microsoft,{CFBFAE00}

也许您最好使用Python的标准

例如:

import csv

data = ["some", "header"], ["and", "data"]

with open("test.csv", "w") as csv_file:
    writer = csv.writer(csv_file)
    writer.writerows(data)
生成文件

some,header
and,data

制表更适合于漂亮的打印。我当然不建议通过使用正则表达式解析tablate的输出来生成CSV文件。

re.sub(“\s+”,“,”,line)
应该这样做。这取代了我的多个空格,但结果只有一行
import csv

data = ["some", "header"], ["and", "data"]

with open("test.csv", "w") as csv_file:
    writer = csv.writer(csv_file)
    writer.writerows(data)