读取一个 excel 文件,为每一行创建一个 txt 文件。我使用的代码只为 excel table 的最后一行创建一个文本文件

Read an excel file, for each row create a txt-file. The code I am using creates only a text file for the last row of the excel table

我是一个完全的初学者,通过应用程序 sololearn 研究了 python 的理论,但直到现在才为我自己使用编码。我也在这里搜索并找到了一些有希望的答案,但并不完全符合我的需求。所以希望大家多多指教

这是我想做的事情: 我有一个 table 文件 (excel),第一列是艺术家姓名,第二列是专辑名称,第三列是关于这张专辑的网络 link。 我希望 python 使用第三列中的 web-link 为每一行创建一个 txt 文件。相关的 txt 文件应使用第一列的艺术家名称和第二列的专辑名称作为新创建的 txt 文件的名称,并在艺术家名称和专辑名称之间添加“-”。

Harald Nordgren 非常友好地为我编写了以下代码:

import xlrd

def replace_with_underscores(cell):
return cell.value.replace(" ", "_")

wb = xlrd.open_workbook("input-file.xlsx")
sh = wb.sheet_by_index(0)

for row in sh.get_rows():
    artist = replace_with_underscores(row[0])
    album = replace_with_underscores(row[1])
    link = row[2].value

filename = artist + "-" + album + ".txt"
with open(filename, 'w') as f:
    f.write(link)

将此代码应用于我的 excel table 仅创建一个新的 txt 文件,其值为 table 最后一行的值。 我真的希望有人能找到这段代码中的错误。

非常感谢 Harald Nordgren 为我编写上述代码。

import xlrd

def replace_with_underscores(cell):
    return cell.value.replace(" ", "_")

wb = xlrd.open_workbook("input-file.xlsx")
sh = wb.sheet_by_index(0)

for row in sh.get_rows():
    artist = replace_with_underscores(row[0])
    album = replace_with_underscores(row[1])
    link = row[2].value

    filename = artist + "-" + album + ".txt"
    with open(filename, 'w') as f:
        f.write(link)