小编Clo*_*oud的帖子

将 csv 输出写入 StringIO 对象

以下代码无法创建 StringIO 对象类型的临时 csv 文件。代码中某处有错误吗?“data_temp”变量不断产生一个空对象。

我使用 StringIO 对象是为了避免在磁盘上创建另一个文件。

from bs4 import BeautifulSoup
from io import StringIO

import csv
import re


# Creates a new csv file to import data to MySQL
def create_csv_file():
    source_html = open(r'C:\\Users\\Admin\\OneDrive\\eCommerce\\Servi-fied\\Raw Data\\EMA - Electricians (Raw).txt', 'r')
    bs_object = BeautifulSoup(source_html, "html.parser")

    data_temp = StringIO()
    csv_file1 = open(r'C:\\Users\\Admin\\OneDrive\\eCommerce\\Servi-fied\\Raw Data\\EMA - Electricians (Processed).csv', 'w+')

    writer1 = csv.writer(data_temp, delimiter='<', skipinitialspace=True)

    table = bs_object.find("table", {"id":"gasOfferSearch"})
    rows = table.findAll("tr")
    # Debugging statement
    print("There are " + (len(rows) - 1).__str__() + " …
Run Code Online (Sandbox Code Playgroud)

python-3.x

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

标签 统计

python-3.x ×1