python csv删除指定行_删除csv文件中的特定行从Python 3.4.3

I'm writing my first Python file which basically grabs some webdata and saves to a .csv file (and working, see below).

The data structure is consistent but has a header consisting of some 17 rows. I want to import the csv into SQL, but its having trouble with the header data, even if I tell it to start reading from row 18 etc it can't see the data unless I manually delete rows 1-17.

I'm thinking the easiest option would be to simply delete rows 1-17 as part of my Python code below. But I have no idea where to start so any tips appreciated.

import urllib.request, urllib.parse, urllib.error

ASXCode = 'CSL'

url = 'http://chartapi.finance.yahoo.com/instrument/1.0/' + ASXCode + '.ax/chartdata;type=quote;range=1d/csv'

urllib.request.urlretrieve(url, "Intra_" + ASXCode + ".csv")

解决方案

You could do it like this which first downloads and saves the webdata into a temporary file, and then copies it to the final destination file but skips the first 17 rows at the beginning.

import csv

import os

import urllib.request, urllib.parse, urllib.error

ASXCode = 'CSL'

local_filename = "Intra_" + ASXCode + ".csv"

url = ('http://chartapi.finance.yahoo.com/instrument/1.0/' + ASXCode +

'.ax/chartdata;type=quote;range=1d/csv')

temp_filename, headers = urllib.request.urlretrieve(url)

with open(temp_filename, 'r', newline='') as inf, \

open(local_filename, 'w', newline='') as outf:

reader = csv.reader(inf)

writer = csv.writer(outf)

for _ in range(17): # skip first 17 rows

next(reader)

writer.writerows(reader) # copy the rest

os.remove(temp_filename) # clean up

print('{} downloaded'.format(local_filename))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值