Skip to content

Instantly share code, notes, and snippets.

@dgadiraju
Created February 4, 2018 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dgadiraju/9c3a71eebbeee3e6f381795f8684495a to your computer and use it in GitHub Desktop.
Save dgadiraju/9c3a71eebbeee3e6f381795f8684495a to your computer and use it in GitHub Desktop.
#String Manipulation
orders = sc.textFile("/public/retail_db/orders")
s = orders.first()
#first character from a string
s[0]
#first 10 characters from a string
s[:10]
#get length of string
len(s)
#One way to get the date, but it will not work if the order id before first
#comma is more than one character or digit
s[2:12]
#split and extract date
s.split(",")
type(s.split(","))
#Get Date
s.split(",")[1]
#Get customer id
s.split(",")[2]
#type casting to integer
int(s.split(",")[0])
#type casting integer to string
print("printing " + str(1))
int(s.split(",")[1].split(" ")[0].replace("-", ""))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment