R Python : Data Read


Read a Text (.txt) file :


LanguageCode
Rfile1 = read.table("mydata.txt")
Pythonfile1 = open("MyFile.txt","a")


Read a Comma separated value (.csv) file :



LanguageCode
Rfile1 = read.csv("mydata.csv",header = TRUE)
Pythonimport CSV
With open(‘some.csv’, ‘rb’) as f:
reader = csv.reader(f)
for row in reader:
print row
Pythonimport pandas as pd
file1 = pd.read_csv("input.csv")


Read a Excel (.xlsx) file :


LanguageCode
Rimport("gdata")
file1 = read.xls("myfile.xlsx"), sheet = 1, header = TRUE)
Pythonfrom openpyxl import load_workbook
ws = wb.active
wb = load_workbook(filename = 'file.xlsx')


Read a XML (.xml) file :



LanguageCode
Rlibrary("XML")
library("methods")
file1 <- xmlParse(file = "input.xml")
Pythonfrom xml.dom import minidom
file1 = minidom.parse('myfile.xml')


Read a HTML (.html) file :


LanguageCode
Rdoc.html = htmlTreeParse('http://apiolaza.net/babel.html',
useInternal = TRUE)

doc.text = unlist(xpathApply(doc.html, '//p', xmlValue))
Pythonimport pandas
file1 = pandas.read_html("input.html")


Read a JSON (.json) file :



LanguageCode
Rlibrary("rjson")
file1 <- fromJSON(file = "input.json")
Pythonimport urllib
sock = urllib.urlopen("http://diveintopython.org/")
htmlSource = sock.read()


Read data from MySQL table :



LanguageCode
Rlibrary(RMySQL)
mydb = dbConnect(MySQL(), user='user', password='password',
dbname='database_name', host='host')
file1 = dbSendQuery(mydb, "select * from user_table")
Pythonimport 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 :


LanguageCode
Rlibrary(RODBC)
channel <- odbcConnect("DATABASE", uid="USERNAME", pwd="PASSWORD")
file1 <- sqlQuery(channel, "SELECT * FROM SCHEMA.DATATABLE")
Pythonimport 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 :



LanguageCode
Rlibrary(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))
Pythonfrom 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

Popular Posts