小编kom*_*waj的帖子

将数据附加到现有的Excel电子表格

我写了以下函数来完成这个任务.

def write_file(url,count):

    book = xlwt.Workbook(encoding="utf-8")
    sheet1 = book.add_sheet("Python Sheet 1")
    colx = 1
    for rowx in range(1):

        # Write the data to rox, column
        sheet1.write(rowx,colx, url)
        sheet1.write(rowx,colx+1, count)


    book.save("D:\Komal\MyPrograms\python_spreadsheet.xls")
Run Code Online (Sandbox Code Playgroud)

对于从给定的.txt文件中获取的每个URL,我希望能够计算标签的数量并将其打印到每个excel文件中.我想覆盖每个URL的文件,然后附加到excel文件.

python excel

3
推荐指数
1
解决办法
2万
查看次数

将文件保存在当前目录的文件夹中

我想创建一个名为当前目录+文件夹+系统日期和时间的文件。我得到的输出为-

D:\Komal\MyPrograms\Pkg\stemwordwww.yahoo.com42015-03-18 16-31
Run Code Online (Sandbox Code Playgroud)

但我想存储名为

www.yahoo.com42015-03-18 16-31 
Run Code Online (Sandbox Code Playgroud)

在文件夹中,stemword即要求输出为

D:\Komal\MyPrograms\Pkg\stemword\www.yahoo.com42015-03-18 16-31
Run Code Online (Sandbox Code Playgroud)

代码

def create_file(self,filename,folder):
    print 'creating file....'
    print 'file is---'
    print filename
    #Here our filename is url eg-www.amazon.in
    dir = os.getcwd()
    dir1 = os.path.join(dir,folder)
    print 'directory---'
    print dir1
    date = datetime.datetime.now()
    now = date.strftime("%Y-%m-%d %H-%M")  
    dirPath2 = os.path.join(dir1+filename)
    dirPath = dirPath2.rstrip('\n')
    filenameCreated = dirPath+now
    print 'file is ---'
    print filenameCreated
    f = self.openfile(filenameCreated + '.txt', 'a')
    f.close()

    return filenameCreated
Run Code Online (Sandbox Code Playgroud)

python

2
推荐指数
1
解决办法
1万
查看次数

标签 统计

python ×2

excel ×1