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

时间:2019-11-14 20:51:09

标签: python 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()

我的CSV输出:

其中•=空格

path•••••name•••••id
C:\Windows•••••Microsoft•••••{CFBFAE00}

预期的CSV输出:

path,name,id
C:\Windows,Microsoft,{CFBFAE00}

2 个答案:

答案 0 :(得分:2)

使用Python的标准可能会更好 csv module

例如:

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

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

答案 1 :(得分:0)

RaisedButton(
            onPressed: () {
              futureAlbum = fetchAlbums(int.parse(_controller.text));
              if (_controller.text.length > 0) {
                this.setState(() {
                  _controller.clear(); // Clear value
                }); // clear the textField
                FocusScope.of(context)
                    .requestFocus(new FocusNode()); // hide the keyboard
              }
            },
            child: Text('Get Album', style: TextStyle(fontSize: 20)),
          ),
相关问题