• <bdo id='v4KWi'></bdo><ul id='v4KWi'></ul>

      <legend id='v4KWi'><style id='v4KWi'><dir id='v4KWi'><q id='v4KWi'></q></dir></style></legend>
    1. <i id='v4KWi'><tr id='v4KWi'><dt id='v4KWi'><q id='v4KWi'><span id='v4KWi'><b id='v4KWi'><form id='v4KWi'><ins id='v4KWi'></ins><ul id='v4KWi'></ul><sub id='v4KWi'></sub></form><legend id='v4KWi'></legend><bdo id='v4KWi'><pre id='v4KWi'><center id='v4KWi'></center></pre></bdo></b><th id='v4KWi'></th></span></q></dt></tr></i><div id='v4KWi'><tfoot id='v4KWi'></tfoot><dl id='v4KWi'><fieldset id='v4KWi'></fieldset></dl></div>

      <tfoot id='v4KWi'></tfoot>

        <small id='v4KWi'></small><noframes id='v4KWi'>

      1. 将有序元组列表保存为 CSV

        时间:2023-08-31
        <legend id='wDHh0'><style id='wDHh0'><dir id='wDHh0'><q id='wDHh0'></q></dir></style></legend>
          <tbody id='wDHh0'></tbody>
      2. <i id='wDHh0'><tr id='wDHh0'><dt id='wDHh0'><q id='wDHh0'><span id='wDHh0'><b id='wDHh0'><form id='wDHh0'><ins id='wDHh0'></ins><ul id='wDHh0'></ul><sub id='wDHh0'></sub></form><legend id='wDHh0'></legend><bdo id='wDHh0'><pre id='wDHh0'><center id='wDHh0'></center></pre></bdo></b><th id='wDHh0'></th></span></q></dt></tr></i><div id='wDHh0'><tfoot id='wDHh0'></tfoot><dl id='wDHh0'><fieldset id='wDHh0'></fieldset></dl></div>
              <bdo id='wDHh0'></bdo><ul id='wDHh0'></ul>

                  <tfoot id='wDHh0'></tfoot>
                1. <small id='wDHh0'></small><noframes id='wDHh0'>

                2. 本文介绍了将有序元组列表保存为 CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个按值排序的元组列表.它们采用 (name,count) 形式,其中count 是每个唯一名称的出现次数.

                  I have a list of tuples ordered by value. They are in the form (name,count) where count is number of occurrences for each unique name.

                  我想将此列表转换为 CSV,其中每个名称都是列标题每个值都是单行的列值.

                  I would like to take this list and transform it into CSV where each name is column header and each value is column value of a single row.

                  有什么建议吗?谢谢.

                  推荐答案

                  你可以这样做:

                  import csv
                  
                  # note: If you use 'b' for the mode, you will get a TypeError
                  # under Python3. You can just use 'w' for Python 3
                  
                  data=[('smith, bob',2),('carol',3),('ted',4),('alice',5)]
                  
                  with open('ur file.csv','wb') as out:
                      csv_out=csv.writer(out)
                      csv_out.writerow(['name','num'])
                      for row in data:
                          csv_out.writerow(row)
                  
                      # You can also do csv_out.writerows(data) instead of the for loop
                  

                  输出文件将有:

                  name,num
                  "smith, bob",2
                  carol,3
                  ted,4
                  alice,5
                  

                  这篇关于将有序元组列表保存为 CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:python如何计算元组的哈希 下一篇:Python AttributeError:“dict"对象没有属性“append"

                  相关文章

                  1. <legend id='qSyEB'><style id='qSyEB'><dir id='qSyEB'><q id='qSyEB'></q></dir></style></legend>
                  2. <tfoot id='qSyEB'></tfoot>

                    1. <i id='qSyEB'><tr id='qSyEB'><dt id='qSyEB'><q id='qSyEB'><span id='qSyEB'><b id='qSyEB'><form id='qSyEB'><ins id='qSyEB'></ins><ul id='qSyEB'></ul><sub id='qSyEB'></sub></form><legend id='qSyEB'></legend><bdo id='qSyEB'><pre id='qSyEB'><center id='qSyEB'></center></pre></bdo></b><th id='qSyEB'></th></span></q></dt></tr></i><div id='qSyEB'><tfoot id='qSyEB'></tfoot><dl id='qSyEB'><fieldset id='qSyEB'></fieldset></dl></div>
                      • <bdo id='qSyEB'></bdo><ul id='qSyEB'></ul>
                    2. <small id='qSyEB'></small><noframes id='qSyEB'>