如何使用python刮片在每个x列之后拆分excel文件中的列?

如何使用python刮片在每个x列之后拆分excel文件中的列?,python,python-2.7,csv,Python,Python 2.7,Csv,我正试图从一个网站上抓取数据并以xls格式保存数据,但当我以xls格式保存数据时,所有数据都在一行中。有人能帮助我如何在10个数据后拆分该行吗?我的代码是 from bs4 import BeautifulSoup import urllib2 pageSource=urllib2.urlopen('http://www.livescore.com/worldcup/tables/').read() soup = BeautifulSoup(pageSource) alltables = s

我正试图从一个网站上抓取数据并以xls格式保存数据,但当我以xls格式保存数据时,所有数据都在一行中。有人能帮助我如何在10个数据后拆分该行吗?我的代码是

from bs4 import BeautifulSoup
import urllib2

pageSource=urllib2.urlopen('http://www.livescore.com/worldcup/tables/').read()

soup = BeautifulSoup(pageSource)
alltables = soup.findAll( "table", {"class":"league-wc table bh"} )

results=[]

for table in alltables:
    rows = table.findAll('tr')
    _table = []
    for tr in rows[1:]:
        _row = []
        cols = tr.findAll('td')
        for td in cols:
            if td.findAll('a'):
                text=td.a.renderContents().strip()
            else:
                text=td.renderContents().strip()
            _row.append(text)
        _table.append(_row)
    results.append(_table)


# print results

index = 1
f=open('world.xls','w')
for table in results:
    for row in table:
        print ','.join([str(index)] + row[1:])
        f.write("\t".join([str(index)] + row[1:]))
        index += 1

f.close()
输出:

1,1,Australia,0,0,0,0,0,0,0,0
2,1,Chile,0,0,0,0,0,0,0,0
3,1,Netherlands,0,0,0,0,0,0,0,0
4,1,Spain,0,0,0,0,0,0,0,0
5,1,Colombia,0,0,0,0,0,0,0,0
6,1,Greece,0,0,0,0,0,0,0,0
7,1,Ivory Coast,0,0,0,0,0,0,0,0
8,1,Japan,0,0,0,0,0,0,0,0
9,1,Costa Rica,0,0,0,0,0,0,0,0
10,1,England,0,0,0,0,0,0,0,0
11,1,Italy,0,0,0,0,0,0,0,0
12,1,Uruguay,0,0,0,0,0,0,0,0
13,1,Ecuador,0,0,0,0,0,0,0,0
14,1,France,0,0,0,0,0,0,0,0
15,1,Honduras,0,0,0,0,0,0,0,0
16,1,Switzerland,0,0,0,0,0,0,0,0
17,1,Argentina,0,0,0,0,0,0,0,0
18,1,Bosnia-Herzegovina,0,0,0,0,0,0,0,0
19,1,Iran,0,0,0,0,0,0,0,0
20,1,Nigeria,0,0,0,0,0,0,0,0
21,1,Germany,0,0,0,0,0,0,0,0
22,1,Ghana,0,0,0,0,0,0,0,0
23,1,Portugal,0,0,0,0,0,0,0,0
24,1,USA,0,0,0,0,0,0,0,0
25,1,Algeria,0,0,0,0,0,0,0,0
26,1,Belgium,0,0,0,0,0,0,0,0
27,1,Russia,0,0,0,0,0,0,0,0
28,1,South Korea,0,0,0,0,0,0,0,0
29,1,Brazil,0,0,0,0,0,0,0,0
30,1,Cameroon,0,0,0,0,0,0,0,0
31,1,Croatia,0,0,0,0,0,0,0,0
32,1,Mexico,0,0,0,0,0,0,0,0
在excel中输出:

1   1   Australia   0   0   0   0   0   0   0   2   1   Chile   0   0   0   0   0   0   0   3   1   Netherlands 0   0   0   0   0   0   0   4   1   Spain   0   0   0   0   0   0   0   5   1   Colombia    0   0   0   0   0   0   0   6   1   Greece  0   0   0   0   0   0   0   7   1   Ivory Coast 0   0   0   0   0   0   0   8   1   Japan   0   0   0   0   0   0   0   9   1   Costa Rica  0   0   0   0   0   0   0   10  1   England 0   0   0   0   0   0   0   11  1   Italy   0   0   0   0   0   0   0   12  1   Uruguay 0   0   0   0   0   0   0   13  1   Ecuador 0   0   0   0   0   0   0   14  1   France  0   0   0   0   0   0   0   15  1   Honduras    0   0   0   0   0   0   0   16  1   Switzerland 0   0   0   0   0   0   0   17  1   Argentina   0   0   0   0   0   0   0   18  1   Bosnia-Herzegovina  0   0   0   0   0   0   0   19  1   Iran    0   0   0   0   0   0   0   20  1   Nigeria 0   0   0   0   0   0   0   21  1   Germany 0   0   0   0   0   0   0   22  1   Ghana   0   0   0   0   0   0   0   23  1   Portugal    0   0   0   0   0   0   0   24  1   USA 0   0   0   0   0   0   0   25  1   Algeria 0   0   0   0   0   0   0   26  1   Belgium 0   0   0   0   0   0   0   27  1   Russia  0   0   0   0   0   0   0   28  1   South Korea 0   0   0   0   0   0   0   29  1   Brazil  0   0   0   0   0   0   0   30  1   Cameroon    0   0   0   0   0   0   0   31  1   Croatia 0   0   0   0   0   0   0   32  1   Mexico  0   0   0   0   0   0   0   0
在excel输出中,上面显示的所有数据都存储在一行中,但我想在每10列之后分割数据,例如在excel输出中的智利之前,以便输出为

1   1   Australia 0 0   0 0 0   0 0  0 2 

Chile           0   0        0  0   0   0   0       3   1   

对脚本的最小更改是在每行末尾的文件中写入一个
“\n”
字符<文件上的code>write()不会添加换行符。例如:

f.write("\t".join([str(index)] + row[1:]) + "\n")

不过,您可能会发现
csv
模块很有用。此外,您可能不应该为制表符分隔值文件提供扩展名
.xls
——它实际上不是Excel文件,即使Excel可以处理它
.tsv
.tab
更为正确,甚至可能
.txt

,我建议对标签进行编辑以澄清这一点。@JulianKnight-编辑您的编辑,因为它是
tsv
而不是
csv
。至少对于OP写入文件而不是打印的内容。我在f.write中得到语法错误和f上的红色突出显示……我如何删除它@PeterdGlopperDoubleCheck您的语法-特别是paren的位置,或前一行中的内容。