1

传递给文档的格式问题。下面的示例 companyList 每次运行脚本时都会有所不同

companyList = ["Apple - Seattle Washington (800) 555-5555", "Microsoft - Tampa Florida (800) 555-1234", "Samsung - Tokyo Japan (01) 555 123-1234"]

现在格式化此文本的代码行是:

companyInfo = "\n\n".join(companyList)

和 companyInfo 输出如下:

Apple - Seattle Washington (800) 555-5555 Microsoft - Tampa Florida (800) 555-1234  Samsung - Tokyo Japan (01) 555 123-1234

我怎样才能将其重写为这样的格式(注意每换行一个标签):

    Apple - Seattle Washington (800) 555-5555
    Microsoft - Tampa Florida (800) 555-1234
    Samsung - Tokyo Japan (01) 555 123-1234

提前谢谢了

4

1 回答 1

1

你可以这样做:

companyInfo = "\n\n".join("\t%s" % x for x in companyList)
于 2013-06-07T19:57:11.910 回答