从CSV文件删除表头

import csv
f = open("example.csv")
f_read = csv.reader(f)
f_data = list(f_read)
#print (f_data)

#print (f_data[2][2])
for row in f_read:#使用for循环,不一次性写入内存
      print ('Row#' + str(f_read.line_num) + ' ' + str(row))
outputFile = open('output.csv', 'w', newline = '')
#注意newline使用
outputWrite =csv.writer(outputFile)
outputWrite.writerow(['a', 'b', 'c', 'd'])
print(outputFile)
outputFile.close()

openfile = open("output.csv")
openfile_red = csv.reader(openfile)
open_data = list(openfile_red)
print (open_data)
#不用逗号分隔 a  b  c ,分隔符
csvWrite = csv.write(csvFile, delimter='\t',lineterminator='\n\n')


]]#! python3
# removeCsvHeader.py - Removes the header from all CSV files in the current
# working directory.

import csv, os

os.makedirs('headerRemoved', exist_ok=True)

# Loop through every file in the current working directory.
for csvFilename in os.listdir('.'):
    if not csvFilename.endswith('.csv'):
        continue # skip non-csv files

    print('Removing header from ' + csvFilename + '...')

    # Read the CSV file in (skipping first row).
    csvRows = []
    csvFileObj = open(csvFilename)
    readerObj = csv.reader(csvFileObj)
    for row in readerObj:
        if readerObj.line_num == 1:
            continue # skip first row
        csvRows.append(row)
    csvFileObj.close()

    # Write out the CSV file.
    csvFileObj = open(os.path.join('headerRemoved', csvFilename), 'w', newline='')
    csvWriter = csv.writer(csvFileObj)
    for row in csvRows:
        csvWriter.writerow(row)
    csvFileObj.close()



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值