0

I'm new to python, this is a fairly simple question. I have a .csv string and I want to remove the first line with the titles:

Date,Open,High,Low,Close,Volume,Adj Close
2016-01-08,1.658,1.70,1.625,1.639,15383400,1.639
2016-01-07,1.64,1.645,1.56,1.642,28015800,1.642
2016-01-06,1.68,1.734,1.672,1.71,15199200,1.71

What's the simplest way to do this? This is how I'm reading the data:

import requests
import csv
import json

theURL = 'http://ichart.yahoo.com/table.csv?s=REC.OL&a=0&b=1&c=2016&d=0&e=10&f=2016'
r = requests.get(theURL)
text = r.text

reader = csv.DictReader(text, fieldnames=("Date", "Open", "High", "Low", "Close", "Volume", "Adj Close"))
jsonText = json.dumps([row for row in reader])

Thanks in advance for your comments!

Rafael Sanchez
  • 394
  • 7
  • 20

1 Answers1

0

If you want to remove the first line, you can do something like this:

string = """Date,Open,High,Low,Close,Volume,Adj Close
2016-01-08,1.658,1.70,1.625,1.639,15383400,1.639
2016-01-07,1.64,1.645,1.56,1.642,28015800,1.642
2016-01-06,1.68,1.734,1.672,1.71,15199200,1.71"""

string = string.split('\n')[1:]

This removes the first line and stores each consecutive line in a list. You can iterate over the list and get your string back or just keep the list for easier use.

Ishaan
  • 707
  • 1
  • 7
  • 16