将字符串导出到Excel中的特定列

将字符串导出到Excel中的特定列

我要学习的是如何使第九列包含Z(指向相应下载文件的链接)
我没有收到任何错误,但是它没有执行我想要执行的操作。
任何帮助都非常感谢。

import csv; import glob; import os
from urllib.request import Request, urlopen
path = 'C:\\Users\\bruno.rojas\\Desktop\\Python_Data\\Test'; extension =
'csv'; os.chdir(path)


file_name = 'TESTEXCEL'
# Creates empty lists that will hold their corresponding values
links = []
def WebGrab(url, filename):
    req = Request(url, headers={"User-Agent": "Mozilla/5.0"})
    webpage = urlopen(req).read()
    text_file = open(filename+'.csv', "wb")
    text_file.write(webpage)
    text_file.close()
    return text_file
with
open('C:\\Users\\bruno.rojas\\Desktop\\Python_Data\\test\\'+file_name+'.csv') as f:
reader = csv.reader(f)
writer = csv.writer(f)
for row in reader:
    links.append("http://chart.finance.yahoo.com/table.csv?s="+row[0]+"&a="+row[3]+"&b="+row[4]+"&c="+row[2]+"&d="+row[7]+"&e="+row[8]+"&f="+row[6]+"&g=d&ignore=.csv")
    for z in links:
        WebGrab(z, row[0])
        for row in reader:
            row[9] = z

最佳答案

尝试这个:

http://xlsxwriter.readthedocs.io/example_demo.html#ex-demo

有下面的例子:

##############################################################################
#
# A simple example of some of the features of the XlsxWriter Python module.
#
# Copyright 2013-2016, John McNamara, [email protected]
#
import xlsxwriter


# Create an new Excel file and add a worksheet.
workbook = xlsxwriter.Workbook('demo.xlsx')
worksheet = workbook.add_worksheet()

# Widen the first column to make the text clearer.
worksheet.set_column('A:A', 20)

# Add a bold format to use to highlight cells.
bold = workbook.add_format({'bold': True})

# Write some simple text.
worksheet.write('A1', 'Hello')

# Text with formatting.
worksheet.write('A2', 'World', bold)

# Write some numbers, with row/column notation.
worksheet.write(2, 0, 123)
worksheet.write(3, 0, 123.456)

# Insert an image.
worksheet.insert_image('B5', 'logo.png')

workbook.close()

关于python - 将字符串导出到Excel中的特定列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43645222/

10-10 16:00