R Python : Data Read
Read a Text (.txt) file :
Language | Code |
R | file1 = read.table("mydata.txt") |
Python | file1 = open("MyFile.txt","a") |
Read a Comma separated value (.csv) file :
Language | Code |
R | file1 = read.csv("mydata.csv",header = TRUE) |
Python | import CSV With open(‘some.csv’, ‘rb’) as f: reader = csv.reader(f) for row in reader: print row |
Python | import pandas as pd file1 = pd.read_csv("input.csv") |
Read a Excel (.xlsx) file :
Language | Code |
R | import("gdata") file1 = read.xls("myfile.xlsx"), sheet = 1, header = TRUE) |
Python | from openpyxl import load_workbook ws = wb.active wb = load_workbook(filename = 'file.xlsx') |
Read a XML (.xml) file :
Language | Code |
R | library("XML") library("methods") file1 <- xmlParse(file = "input.xml") |
Python | from xml.dom import minidom file1 = minidom.parse('myfile.xml') |
Read a HTML (.html) file :
Language | Code |
R | doc.html = htmlTreeParse('http://apiolaza.net/babel.html', useInternal = TRUE) doc.text = unlist(xpathApply(doc.html, '//p', xmlValue)) |
Python | import pandas file1 = pandas.read_html("input.html") |
Read a JSON (.json) file :
Language | Code |
R | library("rjson") file1 <- fromJSON(file = "input.json") |
Python | import urllib sock = urllib.urlopen("http://diveintopython.org/") htmlSource = sock.read() |
Read data from MySQL table :
Language | Code |
R | library(RMySQL) mydb = dbConnect(MySQL(), user='user', password='password', dbname='database_name', host='host') file1 = dbSendQuery(mydb, "select * from user_table") |
Python | import MySQLdb import sys connection = MySQLdb.connect (host = "192.168.1.2", user = "user", passwd = "password, db = "scripting_mysql") cursor = connection.cursor () cursor.execute ("select * from table") |
import mysql.connector |
Read data from Microsoft SQL table :
Language | Code |
R | library(RODBC) channel <- odbcConnect("DATABASE", uid="USERNAME", pwd="PASSWORD") file1 <- sqlQuery(channel, "SELECT * FROM SCHEMA.DATATABLE") |
Python | import cx_Oracle connection = raw_input("Enter Oracle DB connection (uid/pwd@database) : ") orcl = cx_Oracle.connect(connection) curs = orcl.cursor() sql = "select * from tab" curs.execute(sql) |
Read data from Mongo DB table :
Language | Code |
R | library(rmongodb) m <- mongo.create(host = "localhost", db = "example") rawData <- mongo.find.all(m, "example.request", query = list(this = "AUTO"), fields = list(hello = 1L, is = 1L, an = 1L, data.facebook = 1L, the_point = 1L)) |
Python | from pymongo import MongoClient import datetime client = MongoClient("127.0.0.1:8080",username="admin",password="abc") db=client.admin db = client['tutorial'] coll = db['articles'] results = coll.find({"start_date": {'$lt': datetime(2015, 9, 1)}}) for doc in results: print(doc) |
Comments
Post a Comment