python的CSV模块 爱被打了一巴掌 2021-09-22 06:04 262阅读 0赞 # 使用Python的CSV模块读写CSV文件 # 发表于 2015-10-08 | 分类于 [Programming Notes ][Programming Notes] | 热度 2604°C | [0 Comments][] **开发环境:Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:44:40) \[MSC v.1600 64 bit (AMD64)\] on win32** #### 自序 #### 也许你会说,我为什么要学习使用CSV模块呢?没有CSV模块我一样可以解析操作CSV文件,比如下面这种代码: <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed; word-wrap:break-word"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:'Input Mono','PT Mono',Consolas,Monaco,Menlo,monospace; font-size:15px; margin-top:0px; margin-bottom:0px; padding:1px 20px 1px 1px; color:rgb(102,102,102); line-height:1.6; border:none; text-align:right; background:rgb(247,247,247)"><span style="height:20px">1</span> <span style="height:20px">2</span> <span style="height:20px">3</span> <span style="height:20px">4</span> <span style="height:20px">5</span> </pre> </td> <td style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:'Input Mono','PT Mono',Consolas,Monaco,Menlo,monospace; font-size:15px; margin-top:0px; margin-bottom:0px; padding:1px; color:rgb(77,77,76); line-height:1.6; border:none; background:rgb(247,247,247)"><span style="height:20px"><span style="color:rgb(137,89,168)">with</span> open(<span style="color:rgb(113,140,0)">'stocks.csv'</span>) <span style="color:rgb(137,89,168)">as</span> f:</span> <span style="height:20px"><span style="color:rgb(137,89,168)">for</span> line <span style="color:rgb(137,89,168)">in</span> f:</span> <span style="height:20px"> row = line.split(<span style="color:rgb(113,140,0)">','</span>)</span> <span style="height:20px"> <span style="color:rgb(142,144,140)"># process row</span></span> <span style="height:20px"> ...</span> </pre> </td> </tr> </tbody> </table> 使用这种方式的一个缺点就是你仍然需要去处理一些棘手的细节问题。比如,如果某些字段值被引号包围,你不得不去除这些引号。另外,如果一个被引号包围的字段碰巧含有一个逗号,那么程序就会因为产生一个错误而停止。 默认情况下,`CSV`库可识别`Microsoft Excel`所使用的`CSV`编码规则。这或许也是最常见的形式,并且也会给你带来最好的兼容性。然而,如果你查看`CSV`的文档,就会发现有很多种方法将它应用到其他编码格式上(如修改分隔字符等,用Tab分隔)。所以你应该总是优先选择`CSV`模块分割或解析`CSV`数据。 #### CSV格式简介 #### 逗号分隔值(`Comma-Separated Values`,`CSV`,有时也称为字符分隔值,因为分隔字符也可以不是逗号),其文件以纯文本形式存储表格数据(数字和文本)。纯文本意味着该文件是一个字符序列,不含必须像二进制数字那样被解读的数据。`CSV`文件由任意数目的记录组成,记录间以某种换行符分隔;每条记录由字段组成,字段间的分隔符是其它字符或字符串,最常见的是逗号或制表符。通常,所有记录都有完全相同的字段序列。 #### CSV文件注意点 #### 如果你正在读取`CSV`数据并将它们转换为命名元组,需要注意对列名进行合法性认证。一个`CSV`格式文件有一个包含非法标识符的列头行,这样最终会导致在创建一个命名元组时产生一个`ValueError`异常而失败。为了解决这问题,你可能不得不先去修正列标题。 <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed; word-wrap:break-word"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:'Input Mono','PT Mono',Consolas,Monaco,Menlo,monospace; font-size:15px; margin-top:0px; margin-bottom:0px; padding:1px 20px 1px 1px; color:rgb(102,102,102); line-height:1.6; border:none; text-align:right; background:rgb(247,247,247)"><span style="height:20px">1</span> <span style="height:20px">2</span> <span style="height:20px">3</span> <span style="height:20px">4</span> <span style="height:20px">5</span> <span style="height:20px">6</span> <span style="height:20px">7</span> <span style="height:20px">8</span> <span style="height:20px">9</span> </pre> </td> <td style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:'Input Mono','PT Mono',Consolas,Monaco,Menlo,monospace; font-size:15px; margin-top:0px; margin-bottom:0px; padding:1px; color:rgb(77,77,76); line-height:1.6; border:none; background:rgb(247,247,247)"><span style="height:20px"><span style="color:rgb(137,89,168)">import</span> re</span> <span style="height:20px"><span style="color:rgb(137,89,168)">with</span> open(<span style="color:rgb(113,140,0)">'stock.csv'</span>) <span style="color:rgb(137,89,168)">as</span> f:</span> <span style="height:20px"> f_csv = csv.reader(f)</span> <span style="height:20px"> headers = [ re.sub(<span style="color:rgb(113,140,0)">'[^a-zA-Z_]'</span>, <span style="color:rgb(113,140,0)">'_'</span>, h) <span style="color:rgb(137,89,168)">for</span> h <span style="color:rgb(137,89,168)">in</span> next(f_csv) ]</span> <span style="height:20px"> Row = namedtuple(<span style="color:rgb(113,140,0)">'Row'</span>, headers)</span> <span style="height:20px"> <span style="color:rgb(137,89,168)">for</span> r <span style="color:rgb(137,89,168)">in</span> f_csv:</span> <span style="height:20px"> row = Row(*r)</span> <span style="height:20px"> <span style="color:rgb(142,144,140)"># Process row</span></span> <span style="height:20px"> ...</span> </pre> </td> </tr> </tbody> </table> 还有重要的一点需要强调的是,`CSV`产生的数据都是字符串类型的,它不会做任何其他类型的转换。如果你需要做这样的类型转换,你必须自己手动去实现。 #### CSV模块操作示例 #### 输入文件:stocks.csv <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed; word-wrap:break-word"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:'Input Mono','PT Mono',Consolas,Monaco,Menlo,monospace; font-size:15px; margin-top:0px; margin-bottom:0px; padding:1px 20px 1px 1px; color:rgb(102,102,102); line-height:1.6; border:none; text-align:right; background:rgb(247,247,247)"><span style="height:20px">1</span> <span style="height:20px">2</span> <span style="height:20px">3</span> <span style="height:20px">4</span> <span style="height:20px">5</span> <span style="height:20px">6</span> <span style="height:20px">7</span> </pre> </td> <td style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:'Input Mono','PT Mono',Consolas,Monaco,Menlo,monospace; font-size:15px; margin-top:0px; margin-bottom:0px; padding:1px; color:rgb(77,77,76); line-height:1.6; border:none; background:rgb(247,247,247)"><span style="height:20px">Symbol,Price,Date,Time,Change,Volume</span> <span style="height:20px">"AA",39.48,"6/11/2007","9:36am",-0.18,181800</span> <span style="height:20px">"AIG",71.38,"6/11/2007","9:36am",-0.15,195500</span> <span style="height:20px">"AXP",62.58,"6/11/2007","9:36am",-0.46,935000</span> <span style="height:20px">"BA",98.31,"6/11/2007","9:36am",+0.12,104800</span> <span style="height:20px">"C",53.08,"6/11/2007","9:36am",-0.25,360900</span> <span style="height:20px">"CAT",78.29,"6/11/2007","9:36am",-0.23,225400</span> </pre> </td> </tr> </tbody> </table> 输出文件:dest.csv <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed; word-wrap:break-word"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:'Input Mono','PT Mono',Consolas,Monaco,Menlo,monospace; font-size:15px; margin-top:0px; margin-bottom:0px; padding:1px 20px 1px 1px; color:rgb(102,102,102); line-height:1.6; border:none; text-align:right; background:rgb(247,247,247)"><span style="height:20px">1</span> <span style="height:20px">2</span> <span style="height:20px">3</span> <span style="height:20px">4</span> </pre> </td> <td style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:'Input Mono','PT Mono',Consolas,Monaco,Menlo,monospace; font-size:15px; margin-top:0px; margin-bottom:0px; padding:1px; color:rgb(77,77,76); line-height:1.6; border:none; background:rgb(247,247,247)"><span style="height:20px">Symbol,Price,Date,Time,Change,Volume</span> <span style="height:20px">AA,39.48,6/11/2007,9:36am,-0.18,181800</span> <span style="height:20px">AIG,71.38,6/11/2007,9:36am,-0.15,195500</span> <span style="height:20px">AXP,62.58,6/11/2007,9:36am,-0.46,935000</span> </pre> </td> </tr> </tbody> </table> 代码 <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed; word-wrap:break-word"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:'Input Mono','PT Mono',Consolas,Monaco,Menlo,monospace; font-size:15px; margin-top:0px; margin-bottom:0px; padding:1px 20px 1px 1px; color:rgb(102,102,102); line-height:1.6; border:none; text-align:right; background:rgb(247,247,247)"><span style="height:20px">1</span> <span style="height:20px">2</span> <span style="height:20px">3</span> <span style="height:20px">4</span> <span style="height:20px">5</span> <span style="height:20px">6</span> <span style="height:20px">7</span> <span style="height:20px">8</span> <span style="height:20px">9</span> <span style="height:20px">10</span> <span style="height:20px">11</span> <span style="height:20px">12</span> <span style="height:20px">13</span> <span style="height:20px">14</span> <span style="height:20px">15</span> <span style="height:20px">16</span> <span style="height:20px">17</span> <span style="height:20px">18</span> <span style="height:20px">19</span> <span style="height:20px">20</span> <span style="height:20px">21</span> <span style="height:20px">22</span> <span style="height:20px">23</span> <span style="height:20px">24</span> <span style="height:20px">25</span> <span style="height:20px">26</span> <span style="height:20px">27</span> <span style="height:20px">28</span> <span style="height:20px">29</span> <span style="height:20px">30</span> <span style="height:20px">31</span> <span style="height:20px">32</span> <span style="height:20px">33</span> <span style="height:20px">34</span> <span style="height:20px">35</span> <span style="height:20px">36</span> <span style="height:20px">37</span> <span style="height:20px">38</span> <span style="height:20px">39</span> <span style="height:20px">40</span> <span style="height:20px">41</span> <span style="height:20px">42</span> <span style="height:20px">43</span> <span style="height:20px">44</span> <span style="height:20px">45</span> <span style="height:20px">46</span> <span style="height:20px">47</span> <span style="height:20px">48</span> <span style="height:20px">49</span> <span style="height:20px">50</span> <span style="height:20px">51</span> <span style="height:20px">52</span> <span style="height:20px">53</span> <span style="height:20px">54</span> <span style="height:20px">55</span> <span style="height:20px">56</span> <span style="height:20px">57</span> <span style="height:20px">58</span> <span style="height:20px">59</span> <span style="height:20px">60</span> <span style="height:20px">61</span> <span style="height:20px">62</span> <span style="height:20px">63</span> <span style="height:20px">64</span> <span style="height:20px">65</span> <span style="height:20px">66</span> <span style="height:20px">67</span> <span style="height:20px">68</span> <span style="height:20px">69</span> <span style="height:20px">70</span> <span style="height:20px">71</span> <span style="height:20px">72</span> <span style="height:20px">73</span> <span style="height:20px">74</span> <span style="height:20px">75</span> <span style="height:20px">76</span> <span style="height:20px">77</span> <span style="height:20px">78</span> <span style="height:20px">79</span> <span style="height:20px">80</span> <span style="height:20px">81</span> </pre> </td> <td style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:'Input Mono','PT Mono',Consolas,Monaco,Menlo,monospace; font-size:15px; margin-top:0px; margin-bottom:0px; padding:1px; color:rgb(77,77,76); line-height:1.6; border:none; background:rgb(247,247,247)"><span style="height:20px"><span style="color:rgb(142,144,140)">#!/usr/bin/python</span></span> <span style="height:20px"><span style="color:rgb(142,144,140)"># coding: UTF-8</span></span> <span style="height:20px"><span style="color:rgb(113,140,0)">"""</span> <span style="height:20px">Created on 2015/9/30 14:55</span> <span style="height:20px">@author: 'WX'</span> <span style="height:20px">"""</span></span> <span style="height:20px"><span style="color:rgb(137,89,168)">from</span> collections <span style="color:rgb(137,89,168)">import</span> namedtuple</span> <span style="height:20px"><span style="color:rgb(137,89,168)">import</span> csv</span> <span style="height:20px"></span> <span style="height:20px"><span style="color:rgb(137,89,168)">if</span> __name__ == <span style="color:rgb(113,140,0)">"__main__"</span>:</span> <span style="height:20px"> fileName = <span style="color:rgb(113,140,0)">'stocks.csv'</span></span> <span style="height:20px"> writeFileName = <span style="color:rgb(113,140,0)">'dest.csv'</span></span> <span style="height:20px"> <span style="color:rgb(137,89,168)">with</span> open(fileName) <span style="color:rgb(137,89,168)">as</span> f:</span> <span style="height:20px"> reader = csv.reader(f)</span> <span style="height:20px"> headers = next(reader)</span> <span style="height:20px"> print(<span style="color:rgb(113,140,0)">"使用下标进行访问"</span>)</span> <span style="height:20px"> <span style="color:rgb(137,89,168)">for</span> row <span style="color:rgb(137,89,168)">in</span> reader:</span> <span style="height:20px"> <span style="color:rgb(142,144,140)"># 可以使用下标进行访问,row是一个元组</span></span> <span style="height:20px"> length = len(row)</span> <span style="height:20px"> <span style="color:rgb(137,89,168)">for</span> i <span style="color:rgb(137,89,168)">in</span> range(length):</span> <span style="height:20px"> print(row[i], end=<span style="color:rgb(113,140,0)">'\t'</span>)</span> <span style="height:20px"> print()</span> <span style="height:20px"></span> <span style="height:20px"> print(<span style="color:rgb(113,140,0)">'------------------------------------------------'</span>)</span> <span style="height:20px"> <span style="color:rgb(137,89,168)">with</span> open(fileName) <span style="color:rgb(137,89,168)">as</span> f:</span> <span style="height:20px"> <span style="color:rgb(142,144,140)"># 使用命名元组访问</span></span> <span style="height:20px"> reader = csv.reader(f)</span> <span style="height:20px"> headers = next(reader)</span> <span style="height:20px"> Row = namedtuple(<span style="color:rgb(113,140,0)">'Row'</span>, headers)</span> <span style="height:20px"> print(<span style="color:rgb(113,140,0)">"使用命名元组进行访问"</span>)</span> <span style="height:20px"> <span style="color:rgb(137,89,168)">for</span> r <span style="color:rgb(137,89,168)">in</span> reader:</span> <span style="height:20px"> row = Row(*r)</span> <span style="height:20px"> <span style="color:rgb(142,144,140)"># 这时候就可以使用首行的列名来进行访问了</span></span> <span style="height:20px"> line = <span style="color:rgb(113,140,0)">'%s\t%s\t%s\t%s\t%s\t%s'</span> % (row.Symbol, row.Price, row.Date, row.Time, row.Change, row.Volume)</span> <span style="height:20px"> print(line)</span> <span style="height:20px"> print(<span style="color:rgb(113,140,0)">'------------------------------------------------'</span>)</span> <span style="height:20px"></span> <span style="height:20px"> <span style="color:rgb(137,89,168)">with</span> open(fileName) <span style="color:rgb(137,89,168)">as</span> f:</span> <span style="height:20px"> <span style="color:rgb(142,144,140)"># 将内容读取到字典序列中,然后用key去读取</span></span> <span style="height:20px"> dict_reader = csv.DictReader(f)</span> <span style="height:20px"> print(<span style="color:rgb(113,140,0)">"使用字典序列进行访问"</span>)</span> <span style="height:20px"> <span style="color:rgb(137,89,168)">for</span> row <span style="color:rgb(137,89,168)">in</span> dict_reader:</span> <span style="height:20px"> line = <span style="color:rgb(113,140,0)">'%s\t%s\t%s\t%s\t%s\t%s'</span> % (</span> <span style="height:20px"> row[<span style="color:rgb(113,140,0)">'Symbol'</span>], row[<span style="color:rgb(113,140,0)">'Price'</span>], row[<span style="color:rgb(113,140,0)">'Date'</span>], row[<span style="color:rgb(113,140,0)">'Time'</span>], row[<span style="color:rgb(113,140,0)">'Change'</span>], row[<span style="color:rgb(113,140,0)">'Volume'</span>])</span> <span style="height:20px"> print(line)</span> <span style="height:20px"> print(<span style="color:rgb(113,140,0)">'------------------------------------------------'</span>)</span> <span style="height:20px"></span> <span style="height:20px"> print(<span style="color:rgb(113,140,0)">"使用元组方式写入,SUCCESS!"</span>)</span> <span style="height:20px"> <span style="color:rgb(137,89,168)">with</span> open(writeFileName, mode=<span style="color:rgb(113,140,0)">'w'</span>, newline=<span style="color:rgb(113,140,0)">''</span>) <span style="color:rgb(137,89,168)">as</span> wf:</span> <span style="height:20px"> headers = [<span style="color:rgb(113,140,0)">'Symbol'</span>, <span style="color:rgb(113,140,0)">'Price'</span>, <span style="color:rgb(113,140,0)">'Date'</span>, <span style="color:rgb(113,140,0)">'Time'</span>, <span style="color:rgb(113,140,0)">'Change'</span>, <span style="color:rgb(113,140,0)">'Volume'</span>]</span> <span style="height:20px"> rows = [(<span style="color:rgb(113,140,0)">'AA'</span>, <span style="color:rgb(113,140,0)">39.48</span>, <span style="color:rgb(113,140,0)">'6/11/2007'</span>, <span style="color:rgb(113,140,0)">'9:36am'</span>, -<span style="color:rgb(113,140,0)">0.18</span>, <span style="color:rgb(113,140,0)">181800</span>),</span> <span style="height:20px"> (<span style="color:rgb(113,140,0)">'AIG'</span>, <span style="color:rgb(113,140,0)">71.38</span>, <span style="color:rgb(113,140,0)">'6/11/2007'</span>, <span style="color:rgb(113,140,0)">'9:36am'</span>, -<span style="color:rgb(113,140,0)">0.15</span>, <span style="color:rgb(113,140,0)">195500</span>),</span> <span style="height:20px"> (<span style="color:rgb(113,140,0)">'AXP'</span>, <span style="color:rgb(113,140,0)">62.58</span>, <span style="color:rgb(113,140,0)">'6/11/2007'</span>, <span style="color:rgb(113,140,0)">'9:36am'</span>, -<span style="color:rgb(113,140,0)">0.46</span>, <span style="color:rgb(113,140,0)">935000</span>),</span> <span style="height:20px"> ]</span> <span style="height:20px"> writer = csv.writer(wf)</span> <span style="height:20px"> writer.writerow(headers)</span> <span style="height:20px"> writer.writerows(rows)</span> <span style="height:20px"> print(<span style="color:rgb(113,140,0)">"使用字典方式写入,SUCCESS!"</span>)</span> <span style="height:20px"> <span style="color:rgb(142,144,140)"># 在Windows平台需要指定newline='',否则在两行内容之间会多出一行空行</span></span> <span style="height:20px"> <span style="color:rgb(137,89,168)">with</span> open(writeFileName, mode=<span style="color:rgb(113,140,0)">'w'</span>, newline=<span style="color:rgb(113,140,0)">''</span>) <span style="color:rgb(137,89,168)">as</span> wf:</span> <span style="height:20px"> headers = [<span style="color:rgb(113,140,0)">'Symbol'</span>, <span style="color:rgb(113,140,0)">'Price'</span>, <span style="color:rgb(113,140,0)">'Date'</span>, <span style="color:rgb(113,140,0)">'Time'</span>, <span style="color:rgb(113,140,0)">'Change'</span>, <span style="color:rgb(113,140,0)">'Volume'</span>]</span> <span style="height:20px"> rows = [{ <span style="color:rgb(113,140,0)">'Symbol'</span>: <span style="color:rgb(113,140,0)">'AA'</span>, <span style="color:rgb(113,140,0)">'Price'</span>: <span style="color:rgb(113,140,0)">39.48</span>, <span style="color:rgb(113,140,0)">'Date'</span>: <span style="color:rgb(113,140,0)">'6/11/2007'</span>,</span> <span style="height:20px"> <span style="color:rgb(113,140,0)">'Time'</span>: <span style="color:rgb(113,140,0)">'9:36am'</span>, <span style="color:rgb(113,140,0)">'Change'</span>: -<span style="color:rgb(113,140,0)">0.18</span>, <span style="color:rgb(113,140,0)">'Volume'</span>: <span style="color:rgb(113,140,0)">181800</span>},</span> <span style="height:20px"> { <span style="color:rgb(113,140,0)">'Symbol'</span>: <span style="color:rgb(113,140,0)">'AIG'</span>, <span style="color:rgb(113,140,0)">'Price'</span>: <span style="color:rgb(113,140,0)">71.38</span>, <span style="color:rgb(113,140,0)">'Date'</span>: <span style="color:rgb(113,140,0)">'6/11/2007'</span>,</span> <span style="height:20px"> <span style="color:rgb(113,140,0)">'Time'</span>: <span style="color:rgb(113,140,0)">'9:36am'</span>, <span style="color:rgb(113,140,0)">'Change'</span>: -<span style="color:rgb(113,140,0)">0.15</span>, <span style="color:rgb(113,140,0)">'Volume'</span>: <span style="color:rgb(113,140,0)">195500</span>},</span> <span style="height:20px"> { <span style="color:rgb(113,140,0)">'Symbol'</span>: <span style="color:rgb(113,140,0)">'AXP'</span>, <span style="color:rgb(113,140,0)">'Price'</span>: <span style="color:rgb(113,140,0)">62.58</span>, <span style="color:rgb(113,140,0)">'Date'</span>: <span style="color:rgb(113,140,0)">'6/11/2007'</span>,</span> <span style="height:20px"> <span style="color:rgb(113,140,0)">'Time'</span>: <span style="color:rgb(113,140,0)">'9:36am'</span>, <span style="color:rgb(113,140,0)">'Change'</span>: -<span style="color:rgb(113,140,0)">0.46</span>, <span style="color:rgb(113,140,0)">'Volume'</span>: <span style="color:rgb(113,140,0)">935000</span>}, ]</span> <span style="height:20px"> writer = csv.DictWriter(wf, headers)</span> <span style="height:20px"> writer.writeheader()</span> <span style="height:20px"> writer.writerows(rows)</span> <span style="height:20px"> <span style="color:rgb(142,144,140)"># 更换一种分隔符写入文件</span></span> <span style="height:20px"> <span style="color:rgb(137,89,168)">with</span> open(writeFileName, mode=<span style="color:rgb(113,140,0)">'w'</span>, newline=<span style="color:rgb(113,140,0)">''</span>) <span style="color:rgb(137,89,168)">as</span> wf:</span> <span style="height:20px"> headers = [<span style="color:rgb(113,140,0)">'Symbol'</span>, <span style="color:rgb(113,140,0)">'Price'</span>, <span style="color:rgb(113,140,0)">'Date'</span>, <span style="color:rgb(113,140,0)">'Time'</span>, <span style="color:rgb(113,140,0)">'Change'</span>, <span style="color:rgb(113,140,0)">'Volume'</span>]</span> <span style="height:20px"> rows = [(<span style="color:rgb(113,140,0)">'AA'</span>, <span style="color:rgb(113,140,0)">39.48</span>, <span style="color:rgb(113,140,0)">'6/11/2007'</span>, <span style="color:rgb(113,140,0)">'9:36am'</span>, -<span style="color:rgb(113,140,0)">0.18</span>, <span style="color:rgb(113,140,0)">181800</span>),</span> <span style="height:20px"> (<span style="color:rgb(113,140,0)">'AIG'</span>, <span style="color:rgb(113,140,0)">71.38</span>, <span style="color:rgb(113,140,0)">'6/11/2007'</span>, <span style="color:rgb(113,140,0)">'9:36am'</span>, -<span style="color:rgb(113,140,0)">0.15</span>, <span style="color:rgb(113,140,0)">195500</span>),</span> <span style="height:20px"> (<span style="color:rgb(113,140,0)">'AXP'</span>, <span style="color:rgb(113,140,0)">62.58</span>, <span style="color:rgb(113,140,0)">'6/11/2007'</span>, <span style="color:rgb(113,140,0)">'9:36am'</span>, -<span style="color:rgb(113,140,0)">0.46</span>, <span style="color:rgb(113,140,0)">935000</span>),</span> <span style="height:20px"> ]</span> <span style="height:20px"> writer = csv.writer(wf, delimiter=<span style="color:rgb(113,140,0)">';'</span>)</span> <span style="height:20px"> writer.writerow(headers)</span> <span style="height:20px"> writer.writerows(rows)</span> <span style="height:20px"> print(<span style="color:rgb(113,140,0)">"使用分好作为分隔符写入,SUCCESS!"</span>)</span> </pre> </td> </tr> </tbody> </table> 控制台输出 <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed; word-wrap:break-word"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:'Input Mono','PT Mono',Consolas,Monaco,Menlo,monospace; font-size:15px; margin-top:0px; margin-bottom:0px; padding:1px 20px 1px 1px; color:rgb(102,102,102); line-height:1.6; border:none; text-align:right; background:rgb(247,247,247)"><span style="height:20px">1</span> <span style="height:20px">2</span> <span style="height:20px">3</span> <span style="height:20px">4</span> <span style="height:20px">5</span> <span style="height:20px">6</span> <span style="height:20px">7</span> <span style="height:20px">8</span> <span style="height:20px">9</span> <span style="height:20px">10</span> <span style="height:20px">11</span> <span style="height:20px">12</span> <span style="height:20px">13</span> <span style="height:20px">14</span> <span style="height:20px">15</span> <span style="height:20px">16</span> <span style="height:20px">17</span> <span style="height:20px">18</span> <span style="height:20px">19</span> <span style="height:20px">20</span> <span style="height:20px">21</span> <span style="height:20px">22</span> <span style="height:20px">23</span> <span style="height:20px">24</span> <span style="height:20px">25</span> <span style="height:20px">26</span> <span style="height:20px">27</span> <span style="height:20px">28</span> <span style="height:20px">29</span> </pre> </td> <td style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:'Input Mono','PT Mono',Consolas,Monaco,Menlo,monospace; font-size:15px; margin-top:0px; margin-bottom:0px; padding:1px; color:rgb(77,77,76); line-height:1.6; border:none; background:rgb(247,247,247)"><span style="height:20px">C:\Python34\python.exe E:/workspaces/Python3/edu/shu/python/shumo/CsvUtil.py</span> <span style="height:20px">使用下标进行访问</span> <span style="height:20px">AA 39.48 6/11/2007 9:36am -0.18 181800</span> <span style="height:20px">AIG 71.38 6/11/2007 9:36am -0.15 195500</span> <span style="height:20px">AXP 62.58 6/11/2007 9:36am -0.46 935000</span> <span style="height:20px">BA 98.31 6/11/2007 9:36am +0.12 104800</span> <span style="height:20px">C 53.08 6/11/2007 9:36am -0.25 360900</span> <span style="height:20px">CAT 78.29 6/11/2007 9:36am -0.23 225400</span> <span style="height:20px">------------------------------------------------</span> <span style="height:20px">使用命名元组进行访问</span> <span style="height:20px">AA 39.48 6/11/2007 9:36am -0.18 181800</span> <span style="height:20px">AIG 71.38 6/11/2007 9:36am -0.15 195500</span> <span style="height:20px">AXP 62.58 6/11/2007 9:36am -0.46 935000</span> <span style="height:20px">BA 98.31 6/11/2007 9:36am +0.12 104800</span> <span style="height:20px">C 53.08 6/11/2007 9:36am -0.25 360900</span> <span style="height:20px">CAT 78.29 6/11/2007 9:36am -0.23 225400</span> <span style="height:20px">------------------------------------------------</span> <span style="height:20px">使用字典序列进行访问</span> <span style="height:20px">AA 39.48 6/11/2007 9:36am -0.18 181800</span> <span style="height:20px">AIG 71.38 6/11/2007 9:36am -0.15 195500</span> <span style="height:20px">AXP 62.58 6/11/2007 9:36am -0.46 935000</span> <span style="height:20px">BA 98.31 6/11/2007 9:36am +0.12 104800</span> <span style="height:20px">C 53.08 6/11/2007 9:36am -0.25 360900</span> <span style="height:20px">CAT 78.29 6/11/2007 9:36am -0.23 225400</span> <span style="height:20px">------------------------------------------------</span> <span style="height:20px">使用元组方式写入,SUCCESS!</span> <span style="height:20px">使用字典方式写入,SUCCESS!</span> <span style="height:20px"></span> <span style="height:20px">Process finished with exit code 0</span></pre> </td> </tr> </tbody> </table> [Programming Notes]: http://codepub.cn/categories/Programming-Notes/ [0 Comments]: http://codepub.cn/2015/10/08/Using-the-Python-CSV-module-to-read-and-write-CSV-file/#comments
还没有评论,来说两句吧...