1
1
123qwe2015-10-15 15:02:34
Python
123qwe, 2015-10-15 15:02:34

The simplest parsing in python?

What is up, programmer.
Here is the actual code:

from urllib import request
goog_url = "http://real-chart.finance.yahoo.com/table.csv?s=GOOGL.BA&d=9&e=15&f=2015&g=d&a=8&b=26&c=2011&ignore=.csv"

def download_csv_file (csv_file):
    response = request.urlopen(csv_file)
    csv = response.read()
    csv_str = str(csv)
    lines = csv_str.split("\\n")
    dest_url = r'goog.csv'
    fx = open(dest_url,'w')

    for line in lines:
        fx.write(line + "\n")
    fx.close()

download_csv_file(goog_url)

-What does the argument for split mean? I can't find a similar parameter in the documentation.
-What does the symbol 'r' mean in this line "dest_url = r'goog.csv'"

Answer the question

In order to leave comments, you need to log in

4 answer(s)
V
Valery Ryaboshapko, 2015-10-15
@Yonghwa

.split() is a method of a string object, splits a string by a given character or set of characters, returns a list of strings.
The r prefix means that escaping should be ignored inside the string, it should be used "as is" (short for raw). In this particular case, it doesn't matter.

N
Nerevar_soul, 2015-10-15
@Nerevar_soul

split is a string method. Splits a string at the given delimiter.
r - raw string designation.
Generally it is necessary to look the documentation on lines.

U
un1t, 2015-10-15
@un1t

https://docs.python.org/2/library/string.html
stackoverflow.com/questions/2081640/what-exactly-d...
for csv parsing it is better to use
https://docs.python.org/2/ library/csv.html

L
lPolar, 2015-10-16
@lPolar

For parsing csv it is best to use pandas:

import pandas as pd
goog_url = "http://real-chart.finance.yahoo.com/table.csv?s=GOOGL.BA&d=9&e=15&f=2015&g=d&a=8&b=26&c=2011&ignore=.csv"
data = pd.read_csv(goog_url)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question