Home > front end >  PySpark / Python Slicing and Indexing Issue
PySpark / Python Slicing and Indexing Issue

Time:01-01

Can someone let me know how to pull out certain values from a Python output.

I would like the retrieve the value 'ocweeklyreports' from the the following output using either indexing or slicing:

'config': '{"hiveView":"ocweeklycur.ocweeklyreports"}

This should be relatively easy, however, I'm having problem defining the Slicing / Indexing configuation

The following will successfully give me 'ocweeklyreports'

myslice = config['hiveView'][12:30]

However, I need the indexing or slicing modified so that I will get any value after'ocweeklycur'

CodePudding user response:

I'm not sure what output you're dealing with and how robust you're wanting it but if it's just a string you can do something similar to this (for a quick and dirty solution).

input = "Your input"
indexStart = input.index('.')   1 # Get the index of the input at the . which is where you would like to start collecting it
finalResponse = input[indexStart:-2])
print(finalResponse) # Prints ocweeklyreports

Again, not the most elegant solution but hopefully it helps or at least offers a starting point. Another more robust solution would be to use regex but I'm not that skilled in regex at the moment. Let me know if you have any questions or concerns and I can try to address them.

CodePudding user response:

You could almost all of it using regex. See if this helps:

import re
def search_word(di):
  st = di["config"]["hiveView"]
  p = re.compile(r'^ocweeklycur.(?P<word>\w )')
  m = p.search(st)
  return m.group('word')

if __name__=="__main__":
  d = {'config': {"hiveView":"ocweeklycur.ocweeklyreports"}}
  print(search_word(d))
  • Related