投稿    登录
  《Python3网络爬虫开发实战》赠书活动正在进行中!详情请戳赠书活动!欢迎参与!非常感谢!

[Python3网络爬虫开发实战] 5.1.3-CSV文件存储

Python 崔庆才 8524浏览 14评论

CSV,全称为Comma-Separated Values,中文可以叫作逗号分隔值或字符分隔值,其文件以纯文本形式存储表格数据。该文件是一个字符序列,可以由任意数目的记录组成,记录间以某种换行符分隔。每条记录由字段组成,字段间的分隔符是其他字符或字符串,最常见的是逗号或制表符。不过所有记录都有完全相同的字段序列,相当于一个结构化表的纯文本形式。它比Excel文件更加简介,XLS文本是电子表格,它包含了文本、数值、公式和格式等内容,而CSV中不包含这些内容,就是特定字符分隔的纯文本,结构简单清晰。所以,有时候用CSV来保存数据是比较方便的。本节中,我们来讲解Python读取和写入CSV文件的过程。

1. 写入

这里先看一个最简单的例子:

首先,打开data.csv文件,然后指定打开的模式为w(即写入),获得文件句柄,随后调用csv库的writer()方法初始化写入对象,传入该句柄,然后调用writerow()方法传入每行的数据即可完成写入。

运行结束后,会生成一个名为data.csv的文件,此时数据就成功写入了。直接以文本形式打开的话,其内容如下:

可以看到,写入的文本默认以逗号分隔,调用一次writerow()方法即可写入一行数据。用Excel打开的结果如图5-6所示。

图5-6 打开结果

如果想修改列与列之间的分隔符,可以传入delimiter参数,其代码如下:

这里在初始化写入对象时传入delimiter为空格,此时输出结果的每一列就是以空格分隔了,内容如下:

另外,我们也可以调用writerows()方法同时写入多行,此时参数就需要为二维列表,例如:

输出效果是相同的,内容如下:

但是一般情况下,爬虫爬取的都是结构化数据,我们一般会用字典来表示。在csv库中也提供了字典的写入方式,示例如下:

这里先定义3个字段,用fieldnames表示,然后将其传给DictWriter来初始化一个字典写入对象,接着可以调用writeheader()方法先写入头信息,然后再调用writerow()方法传入相应字典即可。最终写入的结果是完全相同的,内容如下:

这样就可以完成字典到CSV文件的写入了。

另外,如果想追加写入的话,可以修改文件的打开模式,即将open()函数的第二个参数改成a,代码如下:

这样在上面的基础上再执行这段代码,文件内容便会变成:

可见,数据被追加写入到文件中。

如果要写入中文内容的话,可能会遇到字符编码的问题,此时需要给open()参数指定编码格式。比如,这里再写入一行包含中文的数据,代码需要改写如下:

这里需要给open()函数指定编码,否则可能发生编码错误。

另外,如果接触过pandas等库的话,可以调用DataFrame对象的to_csv()方法来将数据写入CSV文件中。

2. 读取

我们同样可以使用csv库来读取CSV文件。例如,将刚才写入的文件内容读取出来,相关代码如下:

运行结果如下:

这里我们构造的是Reader对象,通过遍历输出了每行的内容,每一行都是一个列表形式。注意,如果CSV文件中包含中文的话,还需要指定文件编码。

另外,如果接触过pandas的话,可以利用read_csv()方法将数据从CSV中读取出来,例如:

运行结果如下:

在做数据分析的时候,此种方法用得比较多,也是一种比较方便地读取CSV文件的方法。

本节中,我们了解了CSV文件的写入和读取方式。这也是一种常用的数据存储方式,需要熟练掌握。

转载请注明:静觅 » [Python3网络爬虫开发实战] 5.1.3-CSV文件存储

喜欢 (22)or分享 (0)

我的个人微信公众号,联系我请直接在公众号留言即可~

扫码或搜索:进击的Coder

进击的Coder

微信公众号 扫一扫关注

想结交更多的朋友吗?

来进击的Coder瞧瞧吧

进击的Coder

QQ群号 99350970 立即加入

进击的Coder灌水太多?

这里是纯粹的技术领地

激进的Coder

QQ群号 627725766 立即加入

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请狠狠点击下面的

发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
(14)个小伙伴在吐槽
  1. I merely like the worthwhile details an individual source in your posts modal saham. I most certainly will bookmark your site and also check just as before the following often. I am a little selected I'll be informed lots of brand-new stuff suitable the following! Enjoy for an additional!
    extra2018-12-11 01:10 回复
  2. Just want to say your article is as astounding. The clearness in your post is just nice and i could assume you are an expert on this subject. Fine with your permission let me to grab your RSS feed to keep up to date with forthcoming post. Thanks a million and please keep up the gratifying work.
    descargar facebook2018-12-06 20:55 回复
  3. Your article on [Python3????????????????????????] 5.1.3-CSV???????????? | ?????? is very good. I hope you can continue posting many lot post . Viva cuiqingcai.com
    opini saham terbaru2018-12-02 10:01 回复
  4. Hi, Nice article. Likely to situation with your site around net explorer, may well test out the following? Firefox nonetheless is definitely the current market chief and a big component of individuals will leave out of spectacular crafting because of this trouble.. kumpulan vlogger indonesia
  5. Today, while I was at work, my cousin stole my apple ipad and tested to see if it can survive a twenty five foot drop, just so she can be a youtube sensation. My apple ipad is now destroyed and she has 83 views. I know this is entirely off topic but I had to share it with someone!
  6. Excellent post. I was checking constantly this blog and I am impressed! Extremely useful info specifically the last part 🙂 I care for such info a lot. I was seeking this certain info for a very long time. Thank you and good luck.
    Sling TV2018-11-12 11:38 回复
  7. Aw, this was an extremely nice post. Taking a few minutes and actual effort to generate a good article… but what can I say… I put things off a lot and don't manage to get anything done.
    Quest Bars Cheap2018-11-09 11:21 回复
  8. Hi There, Our company creates custom engaging animated presentation videos for businesses like yours to help increase your online exposures. Animated presentation videos help companies explain their products or services more efficiently to potential customers. Potential customers engage more when there’s a video presentation, most of us would rather watch a video that explains a product or a service than to read the content of a website to know what the offer is. I'm interested in creating an animated presentation video for your company to help increase your online exposure! Please reply back to this message and I will send you some of our animated video samples so you can see what I can do to help your business grow this year. Thank you, Steve S
    Steve2018-11-08 15:37 回复
  9. Quest bars cheap fitnesstipsnew1 quest bars cheap 516999410492780544 quest bars cheap Spot on with this write-up, I truly feel this site needs a great deal more attention. I'll probably be returning to see more, thanks for the advice! Quest bars cheap fitnesstipsnew1 quest bars cheap 516999410492780544 quest bars cheap
    quest bars cheap2018-11-05 09:05 回复
  10. Good site you've got here.. It's hard to find good quality writing like yours nowadays. I honestly appreciate people like you! Take care!!
    quest bars cheap2018-11-04 07:05 回复
  11. 为什么使用writer.writerow()产生的csv文件每两行数据之间会有空行?该如何解决
    小白2018-11-02 11:04 回复
    • 可以在open()函数里增加一个参数newline=''
      风雪扑面2018-11-11 23:25 回复
      • 原来这个网站还有中国人,还以为评论都是外国人
        xxd2018-11-18 08:48 回复
  12. Wow, that's what I was exploring for, what a information! existing here at this blog, thanks admin of this website.
    Coconut Oil2018-10-28 14:12 回复