输出中每个字母之间的.csv分隔符 [英] .csv delimiter in between every letter in output

查看:84
本文介绍了输出中每个字母之间的.csv分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在抓取网站以获取某些信息,并且一直将其写入.csv;但是,每当我运行此脚本时:

I'm scraping a website for some information and I've been writing it to a .csv; however whenever I run this script:

import requests
from bs4 import BeautifulSoup
import csv
import time

site = "url"

with open('receipts_10.csv', 'rU') as csvfile:
    reader = csv.reader(csvfile, delimiter=';')
    outfile = 'test.csv'
    ofile = open(outfile,"wb")
    writer = csv.writer(ofile,delimiter=";")
    for row in reader:

        response = requests.post(site, params={'appReceiptNum':row})
        soup = BeautifulSoup(response.text)
        caseStatus = soup.find("h4")
        for string in caseStatus.stripped_strings:
            writer.writerow(string)

我最终在csv中得到以下输出:

I end up with the following output in my csv:

I;n;i;t;i;a;l; ;R;e;v;i;e;w
D;e;c;i;s;i;o;n

有什么想法吗?

推荐答案

csv.writerow(iterable)期望其可迭代

The csv.writerow(iterable) expects an iterable that its elements will be separated with the system (or default) separator.

python中的字符串对象也是可迭代的,字符串中的字符是可迭代的元素,因此,当您将此方法与单独的字符串一起使用时,其字符是分开的。

The string object in python is an iterable too, the characters of the string are the elements of the iterable, so when you use this method with an alone string its characters are separated.

为避免此行为,您可以使用包含字符串的列表或元组。

To avoid this behaviour you can use a list or a tuple that contains your string.

    for string in caseStatus.stripped_strings:
            newString = string.replace(" ", "")
            writer.writerow([newString])

这篇关于输出中每个字母之间的.csv分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆