IndexError:从CSV文件中读取至少必须有一张纸可见

时间:2018-08-03 18:18:44

标签: python excel pandas csv

我有一个如下所示的CSV文件:

first-name,last-name,email-address
Yes,Beth,bethfat@gmail.com
Iran,Dajango,django@me.com
Paul,Walker,info@walker.com
Ray,Johnson,JJRay@gmail.com

我正在尝试将此CSV文件转换为Excel文件,以通过使用Pandas进一步使用。我这样做的方法是从网页中读取数据:

import requests

def get_data(url):
    headers = {
        "Connection": "close",
        "User-Agent": "TBG/Converter (TBG;file-converter)"
    }
    req = requests.get(url, headers=headers)
    return req.content


def parse_html(html):
    retval = {}
    data = html.split("</head>")
    data = data[-1]
    data = data.split("\n")
    retval["header"] = data[0].strip()
    data = data[1:-1]
    emails = set()
    for email in data:
        emails.add(email.split("<br>")[-1])
    retval["userInformation"] = list(emails)
    return retval

并将其写入临时CSV文件:

import pandas as pd

def write_to_xlsx(data_dict, filename):
    tmp_filename = "/tmp/{}.csv".format(rand())
    with open(tmp_filename, "a+") as tmp:
        data = [data_dict["header"]]
        for item in data_dict["userInformation"]:
            item = item.split(",")
            item = ",".join([c.strip() for c in item])
            data.append(item)
        csv_format = "\n".join(data)
        tmp.write(csv_format)
    print tmp_filename
    with pd.ExcelWriter(filename) as ew:
        pd.read_csv(tmp_filename).to_excel(
            ew, index=False, sheet_name=tmp_filename.split(".")[0], encoding="utf-8"
        )
        ew.save()

在编写Excel文件期间,我收到一条错误消息:

Traceback (most recent call last):
  File "converter.py", line 70, in <module>
    write_to_xlsx(sorted_data, config["filename"].format(str(datetime.datetime.now()).split(" ")[0]))
  File "converter.py", line 63, in write_to_xlsx
    ew.save()
  File "/Users/admin/bin/python/printers/venv/mailchimper/lib/python2.7/site-packages/pandas/io/excel.py", line 985, in __exit__
    self.close()
  File "/Users/admin/bin/python/printers/venv/mailchimper/lib/python2.7/site-packages/pandas/io/excel.py", line 989, in close
    return self.save()
  File "/Users/admin/bin/python/printers/venv/mailchimper/lib/python2.7/site-packages/pandas/io/excel.py", line 1018, in save
    return self.book.save(self.path)
  File "/Users/admin/bin/python/printers/venv/mailchimper/lib/python2.7/site-packages/openpyxl/workbook/workbook.py", line 367, in save
    save_workbook(self, filename)
  File "/Users/admin/bin/python/printers/venv/mailchimper/lib/python2.7/site-packages/openpyxl/writer/excel.py", line 284, in save_workbook
    writer.save(filename)
  File "/Users/admin/bin/python/printers/venv/mailchimper/lib/python2.7/site-packages/openpyxl/writer/excel.py", line 266, in save
    self.write_data()
  File "/Users/admin/bin/python/printers/venv/mailchimper/lib/python2.7/site-packages/openpyxl/writer/excel.py", line 95, in write_data
    archive.writestr(ARC_WORKBOOK, write_workbook(self.workbook))
  File "/Users/admin/bin/python/printers/venv/mailchimper/lib/python2.7/site-packages/openpyxl/writer/workbook.py", line 94, in write_workbook
    active = get_active_sheet(wb)
  File "/Users/admin/bin/python/printers/venv/mailchimper/lib/python2.7/site-packages/openpyxl/writer/workbook.py", line 61, in get_active_sheet
    raise IndexError("At least one sheet must be visible")
IndexError: At least one sheet must be visible

是什么原因导致此问题,该如何解决?

0 个答案:

没有答案