在python中,我们可以保存由用户指定名称提供的CSV文件吗?

时间:2018-08-24 05:10:44

标签: python-2.7 csv getopt

我需要在python中从pandas数据框保存/写入一个csv文件。我已经尝试过以下方式;

import sys
import getopt
import os
def usage():
    print "-f Please provide input file"

in1_flag = False
out_flag = True
inFile1 = ""
outFile1 = ""

try:
    opts, args = getopt.getopt(sys.argv[1:], 'i:o', ['input_file1=', 'out_file1='])
except getopt.GetoptError:
    usage()
    sys.exit(2)

for opt, arg in opts:
    if opt in ('-i', '--input_file1'):
       inFile1 = os.path.abspath(arg)
       in1_flag = True
    elif opt in ('-o', '--out_file1'):
       outFile1 = os.path.abspath(arg)
split_h = []
with open(inFile1) as ff:
    for line in ff:
        split_h = line.split()

import pandas as pd
d1 = {'report': split_h}
df1 = pd.DataFrame(data = d1, columns=['report'])
df1.to_csv(outFile1, sep = '\t',header = False, index= False)

我认为这和输入一样容易,但是在这里我被困在写文件上。

0 个答案:

没有答案