簡體   English   中英

PySpark / Python 切片和索引問題

[英]PySpark / Python Slicing and Indexing Issue

誰能告訴我如何從 Python output 中提取某些值。

我想使用索引或切片從以下 output 中檢索值“ocweeklyreports”:

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

這應該相對容易,但是,我在定義切片/索引配置時遇到問題

以下將成功給我“ocweeklyreports”

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

但是,我需要修改索引或切片,以便在“ocweeklycur”之后獲得任何值

我不確定你正在處理什么 output 以及你想要它有多健壯,但如果它只是一個字符串,你可以做類似的事情(快速而骯臟的解決方案)。

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

同樣,這不是最優雅的解決方案,但希望它能有所幫助或至少提供一個起點。 另一個更強大的解決方案是使用正則表達式,但我目前對正則表達式還不是很熟練。 如果您有任何問題或疑慮,請告訴我,我可以嘗試解決。

您幾乎可以使用正則表達式完成所有操作。 看看這是否有幫助:

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))

以下最適合我:


# Extract the value of the "hiveView" key
hive_view = config['hiveView']

# Split the string on the '.' character
parts = hive_view.split('.')

# The value you want is the second part of the split string
desired_value = parts[1]

print(desired_value)  # Output: "ocweeklyreports"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM