• <legend id='NNzwL'><style id='NNzwL'><dir id='NNzwL'><q id='NNzwL'></q></dir></style></legend>
  • <tfoot id='NNzwL'></tfoot>

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

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

        如何向 CSV 文件添加新列?

        时间:2024-04-21
        <i id='Tl6fP'><tr id='Tl6fP'><dt id='Tl6fP'><q id='Tl6fP'><span id='Tl6fP'><b id='Tl6fP'><form id='Tl6fP'><ins id='Tl6fP'></ins><ul id='Tl6fP'></ul><sub id='Tl6fP'></sub></form><legend id='Tl6fP'></legend><bdo id='Tl6fP'><pre id='Tl6fP'><center id='Tl6fP'></center></pre></bdo></b><th id='Tl6fP'></th></span></q></dt></tr></i><div id='Tl6fP'><tfoot id='Tl6fP'></tfoot><dl id='Tl6fP'><fieldset id='Tl6fP'></fieldset></dl></div>

          <tfoot id='Tl6fP'></tfoot>

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

            <bdo id='Tl6fP'></bdo><ul id='Tl6fP'></ul>
                  <tbody id='Tl6fP'></tbody>
                <legend id='Tl6fP'><style id='Tl6fP'><dir id='Tl6fP'><q id='Tl6fP'></q></dir></style></legend>

                  本文介绍了如何向 CSV 文件添加新列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有几个 CSV 文件,如下所示:

                  I have several CSV files that look like this:

                  Input
                  Name        Code
                  blackberry  1
                  wineberry   2
                  rasberry    1
                  blueberry   1
                  mulberry    2
                  

                  我想为所有 CSV 文件添加一个新列,使其如下所示:

                  I would like to add a new column to all CSV files so that it would look like this:

                  Output
                  Name        Code    Berry
                  blackberry  1   blackberry
                  wineberry   2   wineberry
                  rasberry    1   rasberry
                  blueberry   1   blueberry
                  mulberry    2   mulberry
                  

                  我目前的脚本是这样的:

                  The script I have so far is this:

                  import csv
                  with open(input.csv,'r') as csvinput:
                      with open(output.csv, 'w') as csvoutput:
                          writer = csv.writer(csvoutput)
                          for row in csv.reader(csvinput):
                              writer.writerow(row+['Berry'])
                  

                  (Python 3.2)

                  (Python 3.2)

                  但在输出中,脚本跳过每一行,新列中只有 Berry:

                  But in the output, the script skips every line and the new column has only Berry in it:

                  Output
                  Name        Code    Berry
                  blackberry  1   Berry
                  
                  wineberry   2   Berry
                  
                  rasberry    1   Berry
                  
                  blueberry   1   Berry
                  
                  mulberry    2   Berry
                  

                  推荐答案

                  这应该让你知道该怎么做:

                  This should give you an idea of what to do:

                  >>> v = open('C:/test/test.csv')
                  >>> r = csv.reader(v)
                  >>> row0 = r.next()
                  >>> row0.append('berry')
                  >>> print row0
                  ['Name', 'Code', 'berry']
                  >>> for item in r:
                  ...     item.append(item[0])
                  ...     print item
                  ...     
                  ['blackberry', '1', 'blackberry']
                  ['wineberry', '2', 'wineberry']
                  ['rasberry', '1', 'rasberry']
                  ['blueberry', '1', 'blueberry']
                  ['mulberry', '2', 'mulberry']
                  >>> 
                  

                  编辑,注意在py3k中你必须使用next(r)

                  Edit, note in py3k you must use next(r)

                  感谢您接受答案.在这里你有一个奖励(你的工作脚本):

                  Thanks for accepting the answer. Here you have a bonus (your working script):

                  import csv
                  
                  with open('C:/test/test.csv','r') as csvinput:
                      with open('C:/test/output.csv', 'w') as csvoutput:
                          writer = csv.writer(csvoutput, lineterminator='
                  ')
                          reader = csv.reader(csvinput)
                  
                          all = []
                          row = next(reader)
                          row.append('Berry')
                          all.append(row)
                  
                          for row in reader:
                              row.append(row[0])
                              all.append(row)
                  
                          writer.writerows(all)
                  

                  请注意

                  1. csv.writer 中的lineterminator 参数.默认情况下是设置为 ' ' 这就是你有双倍间距的原因.
                  2. 使用列表附加所有行并将它们写入writerows 的一张照片.如果你的文件非常非常大,这个可能不是一个好主意(RAM),但对于普通文件,我认为它是更快,因为 I/O 更少.
                  3. 正如这篇文章的评论中所指出的,请注意,而不是嵌套两个 with 语句,您可以在同一行中进行:

                  1. the lineterminator parameter in csv.writer. By default it is set to ' ' and this is why you have double spacing.
                  2. the use of a list to append all the lines and to write them in one shot with writerows. If your file is very, very big this probably is not a good idea (RAM) but for normal files I think it is faster because there is less I/O.
                  3. As indicated in the comments to this post, note that instead of nesting the two with statements, you can do it in the same line:

                  用 open('C:/test/test.csv','r') 作为 csvinput,open('C:/test/output.csv', 'w') 作为 csvoutput:

                  with open('C:/test/test.csv','r') as csvinput, open('C:/test/output.csv', 'w') as csvoutput:

                  这篇关于如何向 CSV 文件添加新列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 Python 中用一个字符串和一个整数创建一个字符串 下一篇:如何在子图中绘制多个 Seaborn 联合图

                  相关文章

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

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

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