简体   繁体   English

如何从 .csv 网络文件中提取数据并存储到变量中,并可以选择稍后将其存储在数据库中

[英]how to pul data from a .csv web file and store to a variable with the option to store it later in a database

I'm start to learn python and need your help.我开始学习python,需要你的帮助。 I program in my last time only with Matlab.我最后一次只用 Matlab 编程。

I want to pul as a while loop data form a .csv web file and if the date variable change, append the data to the old and store it in an (cell-)array with the option later to store it in the database.我想将while循环数据从.csv网络文件中提取出来,如果日期变量发生变化,则将数据附加到旧数据并将其存储在(单元格)数组中,稍后可以选择将其存储在数据库中。

So my code in python:所以我在python中的代码:

import csv
import urllib.request

url = "http://download.finance.yahoo.com/d/quotes.csv?s=AGRO.BA,PR15.BA,&f=sl1d1t1c1ohgv&e=.csv"
ftpstream = urllib.request.urlopen(url)
csvfile = csv.reader(ftpstream.read().decode('utf-8')) #with the appropriate encoding
data = [row for row in csvfile]

the output is: (the data means: tick, value, date, time, change, mean, high, low, value)输出是:(数据的意思是:刻度、值、日期、时间、变化、平均值、高、低、值)

['AGRO.BA', '3.14', '10/28/2015', '4:49pm', '+0.09', '3.05', '3.25', '3.00', '150988'] ['BA-C.BA', '115.00', '10/21/2015', '11:41am', '+0.00', '115.00', '115.00', '115.00', '100'] ... ['AGRO.BA', '3.14', '10/28/2015', '4:49pm', '+0.09', '3.05', '3.25', '3.00', '150988'] ['BA- C.BA', '115.00', '10/21/2015', '11:41am', '+0.00', '115.00', '115.00', '115.00', '100'] ...

As I can understand it in matlab, python save data in 2 rows as strings in data.正如我在 matlab 中可以理解的那样,python 将 2 行中的数据保存为数据中的字符串。

1. Question: How can I save it in data in cellarrays with different variable types? 1. 问:如何将数据保存在不同变量类型的cellarray中?

2. Question: How can I save the date as an datenum (datenumber)? 2. 问:如何将日期保存为datenum(datenumber)?

3. Question: How can I change the format of time in 24h format without 'am' or 'pm' and so store it as time or double not as string? 3. 问题:如何在没有'am'或'pm'的情况下更改24小时格式的时间格式,从而将其存储为时间或双倍而不是字符串?

4. Question: How can I pul the data in while loop and if the time change, the program load the old stored data(cell-)array and attend the new data to the old data (not overwrite but attend to the old!)? 4.问题:如何在while循环中pull数据,如果时间改变,程序加载旧存储的数据(cell-)数组并将新数据加入旧数据(不是覆盖而是关注旧数据!) ?

Thanks!谢谢!

Sigh.叹。 What you're supposed to do if someone posts a pointer like that, is use Google to find documents relating to "Python" and "pickle".如果有人发布这样的指针,您应该做的是使用 Google 查找与“Python”和“pickle”相关的文档。 Without knowing it's called pickle, it takes a lot longer to find the python documentation for what you need.在不知道它被称为泡菜的情况下,找到所需的 python 文档需要更长的时间。

However, for the benefit of posterity, here's the absolute basics, as two separate interpreter sessions但是,为了后代的利益,这里是绝对的基础知识,作为两个单独的口译会话

Example of saving some Python objects (a dict and a list-structure), in a way that allows the objects to be recreated later以允许稍后重新创建对象的方式保存一些 Python 对象(字典和列表结构)的示例

 >>> d=dict(a=1,c=3,e=5)
 >>> d
 {'a': 1, 'c': 3, 'e': 5}
 >>> L=['hello','goodbye',['a','list','in','a','list']]

 >>> f = open('temp.tmp','wb')  # open a pickle file
 >>> import pickle
 >>> pickle.dump( d, f)  # save d to the pickle file
 >>> pickle.dump( L, f)  # followed by 
 >>> f.close()

and exit.并退出。 What's in the file can now be read back in by another Python program.文件中的内容现在可以被另一个 Python 程序读回。 Note that there are multiple versions of the pickle protocol, so if you want to read in to a different version of Python to the one that did the writing, you may need to find out about the optional protocol argument to the pickle functions.请注意,pickle 协议有多个版本,因此如果您想读入与编写该版本不同的 Python 版本,您可能需要了解 pickle 函数的可选protocol参数。 That aside, let's read that file ....除此之外,让我们阅读该文件....

>>> import pickle 
>>> f=open('temp.tmp','rb')
>>> something=pickle.load(f)
>>> something
{'c': 3, 'a': 1, 'e': 5}
>>> something2 = pickle.load(f)
>>> something2
['hello', 'goodbye', ['a', 'list', 'in', 'a', 'list']]
>>> 

You can pickle most sorts of Python objects but not all of them.您可以腌制大多数类型的 Python 对象,但不是全部。 Lists and dicts and nests of them are fine.列表和字典以及它们的嵌套都很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM