Delete specific rows in csv file from Python 3.4.3

扶醉桌前 提交于 2019-12-13 07:24:42

问题


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")

回答1:


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))


来源:https://stackoverflow.com/questions/32165972/delete-specific-rows-in-csv-file-from-python-3-4-3

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!