Python:使用自定义格式读取CSV并写入文件

时间:2017-03-23 10:39:19

标签: python csv

我有这个.csv文件......

id,first_name,last_name,email,date,opt-in
1,Jimmy,Reyes,jreyes0@macromedia.com,12/29/2016,FALSE
2,Doris,Wood,dwood1@1und1.de,04/22/2016,
3,Steven,Miller,smiller2@go.com,07/31/2016,FALSE
4,Earl,Parker,eparker3@ucoz.com,01-08-17,FALSE
5,Barbara,Cruz,bcruz4@zdnet.com,12/30/2016,FALSE

我想阅读上面显示的csv文件,转换数据,最后将数据写入另一个文本文件中,这应该是这样的....

1,<tab>"first_name"="Jimmy","last_name"="Reyes","email"="jreyes0@macromedia.com","date"="12/29/2016","opt-in"="FALSE"
2,<tab>"first_name"="Doris","last_name"="Wood","email"="dwood1@1und1.de","date"="04/22/2016,,"opt-in"="0"

此外,如果选择加入值为空,则应打印“0”。

到目前为止,这是我的代码......

import csv
import time

# Do the reading
with open('my-scripts/mock.csv', 'r') as f1:
 #next(f1, None)  # skip the headers
 reader = csv.reader(f1)
 new_rows_list = []
 for row in reader:
   if row[5] == '':
      new_row = [row[0],'\t',row[1], row[2], row[3], row[4], '0']
      new_rows_list.append(new_row)
   else:
      new_row = [row[0],'\t',row[1], row[2], row[3], row[4], row[5]]
      new_rows_list.append(new_row)   
 f1.close()   # <---IMPORTANT

# Do the writing
newfilename = 'my-scripts/ftp_745198_'+str(int(time.time()))
with open(newfilename, 'w', newline='') as f2:
 writer = csv.writer(f2, quoting=csv.QUOTE_NONNUMERIC)
 writer.writerows(new_rows_list)
 f2.close()

上面的代码生成了这个输出,这不是我想要的...... 我无法弄清楚如何在每行中打印列名,如上图所示的所需输出......!< / strong>

"id","  ","first_name","last_name","email","date","opt-in"
"1","   ","Jimmy","Reyes","jreyes0@macromedia.com","12/29/2016","FALSE"
"2","   ","Doris","Wood","dwood1@1und1.de","04/22/2016","0"
"3","   ","Steven","Miller","smiller2@go.com","07/31/2016","FALSE"
"4","   ","Earl","Parker","eparker3@ucoz.com","01-08-17","FALSE"
"5","   ","Barbara","Cruz","bcruz4@zdnet.com","12/30/2016","FALSE"

新CSV

id,first_name,last_name,email,date,opt-in,unique_code
1,Jimmy,Reyes,jreyes0@macromedia.com,12/29/2016,FALSE,ER45DH
2,Doris,Wood,dwood1@1und1.de,04/22/2016,,MU34T3
3,Steven,Miller,smiller2@go.com,07/31/2016,FALSE,G34FGH
4,Earl,Parker,eparker3@ucoz.com,01-08-17,FALSE,ASY67J
5,Barbara,Cruz,bcruz4@zdnet.com,12/30/2016,FALSE,NHG67P

新的预期输出

ER45DH<tab>"id"="1","first_name"="Jimmy","last_name"="Reyes","email"="jreyes0@macromedia.com","date"="12/29/2016","opt-in"="FALSE"
MU34T3<tab>"id"="2","first_name"="Doris","last_name"="Wood","email"="dwood1@1und1.de","date"="04/22/2016,"opt-in"="0"

我将非常感谢任何帮助/想法/指针。

由于

3 个答案:

答案 0 :(得分:1)

您可以将标题保留在列表中,然后使用列表(如first_name等)匹配后续行中的元素(如Jimmy等)以生成您想要的输出(&#34;如first_name&#34; =&#34;麦&#34;。)

答案 1 :(得分:1)

首先,将标题保存到变量中。例如:

for i,row in enumerate(reader):
    if i == 0:
        header = row
    else:
        new_row = [row[0],'\t'] + ['%s=%s' % (header[j],row[j]) for j in range(1,6)]
        ....
...

其次,[row[1], row[2], row[3], row[4], row[5]]之类的代码可以简化为[row [i] for i in range(1,6)](generator

第三,format是一个很好的工具: print('"%s"="%s"'% (header[1],row[1]))将输出"first_name"="Jimmy"

使用这些知识并考虑如何使其发挥作用。

答案 2 :(得分:1)

  • 最初将标题解压缩为新列表。

  • 然后将每个行元素的标题追加为字符串。

  • 将其写入文件。

请尝试使用此代码,

var str = '<div>test</div><p>test</p><span class="removedata">X</span><span>test</span><span class="removedata">X</span>',
    str2 = str.replace(/<span class="removedata">X<\/span>/g, '');
    console.log(str2);

<强>输出:

import csv

with open('newfilename.csv', 'w') as f2:
    with open('mycsvfile.csv', mode='r') as infile:
        reader = csv.reader(infile)
        for i,rows in enumerate(reader):
            if i == 0:
               header = rows 
            else:
                if rows[5] == '':
                   rows[5] = 0;
                pat = rows[0]+'\t'+'''"%s=%%s",'''*(len(header)-1)+'\n'
                print pat
                f2.write(pat % tuple(header[1:]) % tuple(rows[1:]))
    f2.close()

如有任何疑问,请与我们联系。