资源描述:
《retrieving processing and storing data》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、Retrieving,Processing,andStoringDataDatacanbefoundeverywhereinallshapesandforms.WecangetitfromtheWeb,bye-mailandFTP,orcreateitourselvesinalabexperimentormarketingpoll.Anexhaustiveoverviewofhowtoacquiredatainvariousformatswillrequiremanymorepagesthanwhatwehaveavailable.Sometimes,
2、weneedtostoredatabeforewecananalyzeitorafterwearedonewithouranalysis.Wewillalsodiscussstoringdatainthischapter.Chapter8,WorkingwithDatabases,givesinformationaboutvariousdatabases(relationalandNoSQL)andrelatedAPIs.Thefollowingisalistofthetopicsthatwearegoingtocoverinthischapter:•
3、WritingCSVfileswithNumPyandpandas•Thebinary.npyandpickleformats•ReadingandwritingtoExcelwithpandas•JSON•RESTwebservices•ParsingRSSfeeds•ScrapingtheWeb•ParsingHTML•StoringdatawithPyTables•HDF5pandasI/ORetrieving,Processing,andStoringDataWritingCSVfileswithNumPyandpandasIntheprevi
4、ouschapters,welearnedaboutreadingCSVfiles.WritingCSVfilesisjustasstraightforward,butusesdifferentfunctionsandmethods.Let'sfirstgeneratesomedatatobestoredintheCSVformat.Generatea3x4NumPyarrayafterseedingtherandomgeneratorinthefollowingcodesnippet.SetoneofthearrayvaluestoNaN:np.ra
5、ndom.seed(42)a=np.random.randn(3,4)a[2][2]=np.nanprintaThiscodewillprintthearrayasfollows:[[0.49671415-0.13826430.647688541.52302986][-0.23415337-0.234136961.579212820.76743473][-0.469474390.54256004nan-0.46572975]]TheNumPysavetxt()functionisthecounterpartoftheNumPyloadtxt()func
6、tionandcansavearraysindelimitedfileformatssuchasCSV.Savethearraywecreatedwiththefollowingfunctioncall:np.savetxt('np.csv',a,fmt='%.2f',delimiter=',',header="#1,#2,#3,#4")Intheprecedingfunctioncall,wespecifiedthenameofthefiletobesaved,thearray,anoptionalformat,adelimiter(thedefau
7、ltisspace),andanoptionalheader.Theformatparameterisdocumentedathttp://docs.python.org/2/library/string.html#format-specification-mini-language.Viewthenp.csvfilewecreatedwiththecatcommand(catnp.csv)oraneditor,suchasNotepadonWindows.Thecontentsofthefileshouldbedisplayedasfollows:#
8、#1,#2,#3,#40.50,-0.14,0.65,1.52-0.23,-0.23,1.58