Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将表格的列从CSV中移出?_Python_Csv - Fatal编程技术网

Python 如何将表格的列从CSV中移出?

Python 如何将表格的列从CSV中移出?,python,csv,Python,Csv,我已经为CSV数据添加了新的标题(参数),新的参数必须进行计算,所以它需要保持为空。我需要将整个数据向右移动一列,但我不知道如何使用PythonV3.2 谁能帮帮我吗 因此,当我用excel打开python CSV文件时,它会给我类似的信息(超级简化版,我有数千行和数千列) 。 我想把这个改成;让P1空着 P1 P2 P3 P4 P5 ... 1 2 3 4 1 2 3 4 1 2 3 4 这是我到目前为止得到的代码。我只想将数据向右

我已经为CSV数据添加了新的标题(参数),新的参数必须进行计算,所以它需要保持为空。我需要将整个数据向右移动一列,但我不知道如何使用PythonV3.2 谁能帮帮我吗

因此,当我用excel打开python CSV文件时,它会给我类似的信息(超级简化版,我有数千行和数千列)

我想把这个改成;让P1空着

P1 P2 P3 P4 P5 ...

    1  2  3  4  
    1  2  3  4   
    1  2  3  4  
这是我到目前为止得到的代码。我只想将数据向右移动一列。谁能帮帮我吗? 提前谢谢

    import csv

    # reads the input and spits out the output
    with open ('DownloadDB_CSV1.csv', 'r') as csvinput:
         with open ('outputCSV.csv', 'w', newline ='') as csvoutput:
             reader = csv.reader(csvinput, delimiter = ',')
            writer = csv.writer(csvoutput,  delimiter = ',')
    all = []
    row = next(reader)
    row.insert(0,'GenomePosition')
    all.append(row)



    for row in reader:
        all.append(row)
        contents = row[0::] # sepearte it to a variable
    writer.writerows(all)

要插入空列,您只需在每行中添加一个首字母
。这里有一些代码可以做到这一点-你甚至不需要CSV模块来完成它。从你的例子来看,我想你不想改变标题,对吧

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#  test_csv.py
#  
#  Copyright 2015 John Coppens <john@jcoppens.com>
#  
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#  
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#  
#  

SEPARATOR = ","

def add_empty(infile, outfile):
    with open(outfile, "w") as outf:
        with open(infile, "r") as inf:
            line = inf.readline()       # Copy the header
            outf.write(line)

            while True:
                line = inf.readline()
                if line == "": break    # EOF
                outf.write("" + SEPARATOR + line)

def main():
    add_empty("test.csv", "testshifted.csv")
    return 0

if __name__ == '__main__':
    main()
#/usr/bin/env python
#-*-编码:utf-8-*-
#
#test_csv.py
#  
#版权所有2015 John Coppens
#  
#这个程序是自由软件;您可以重新分发和/或修改它
#它是根据GNU通用公共许可证的条款发布的
自由软件基金会;许可证的第2版,或
#(由您选择)任何更高版本。
#  
#这个节目的发布是希望它会有用,
#但无任何保证;甚至没有任何关于
#适销性或适合某一特定目的。见
#有关更多详细信息,请参阅GNU通用公共许可证。
#  
#您应该已经收到GNU通用公共许可证的副本
#与此同时,;如果没有,请写信给自由软件
波士顿基金会51楼富兰克林街第五楼
#MA 02110-1301,美国。
#  
#  
分隔符=“,”
def添加_空(填充、输出文件):
开放式(输出文件,“w”)作为输出:
以open(infle,“r”)作为inf:
line=inf.readline()#复制标题
输出写入(行)
尽管如此:
line=inf.readline()
如果行=“”:中断#EOF
输出写入(“+”分隔符+行)
def main():
添加_empty(“test.csv”、“testshift.csv”)
返回0
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
main()

怎么做?你是说excel?我需要将python脚本中的数组转换为excel中的数组。好的,您使用的是正确的数组,但您仍然从一开始就编写它。所以我的问题是:exel中的空collumn是什么?你为什么不写呢?我是python新手,我一直在用matlab做所有的矩阵运算,,,所以我想不出任何方法将这些数据移动到下一列,,,,,,你想要我做什么新列,一个空字符串?谢谢,这正是我需要的!
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#  test_csv.py
#  
#  Copyright 2015 John Coppens <john@jcoppens.com>
#  
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#  
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#  
#  

SEPARATOR = ","

def add_empty(infile, outfile):
    with open(outfile, "w") as outf:
        with open(infile, "r") as inf:
            line = inf.readline()       # Copy the header
            outf.write(line)

            while True:
                line = inf.readline()
                if line == "": break    # EOF
                outf.write("" + SEPARATOR + line)

def main():
    add_empty("test.csv", "testshifted.csv")
    return 0

if __name__ == '__main__':
    main()