英文:
ValueError: substring not found when refactoring PySpark to work with snowpark
问题
我正在尝试在重构 PySpark 代码以适配 Snowflake 时,建立在我从 Stack Overflow 社区成员那里收到的帮助的基础上。
有人可以看一下以下的 snowpark Python 代码并告诉我为什么我会遇到无效的语法错误吗?
import snowflake.snowpark as snowpark
from snowflake.snowpark.functions import col
def registerDelta(session: snowpark.Session, regName, stage, filePath):
pos = regName.index(".")
if pos > 0:
dbName = stage + regName[0:pos]
else:
dbName = stage
try:
crtdb = f"CREATE DATABASE IF NOT EXISTS {dbName}"
session.sql(crtdb)
print(f"{stage} - Database created")
except:
print("Create Database Failed")
try:
SQL = f"CREATE TABLE IF NOT EXISTS {stage}{regName} USING DELTA LOCATION '{filePath}'"
session.sql(SQL)
print(f"{stage}{regName} - Table created")
except:
print("Create Table Failed")
def main(session: snowpark.Session):
return registerDelta(session, 'test1', 'test2', 'test3')
我有一种感觉这将凸显出我在Python编程方面的不足。不管怎样,有什么想法吗?
英文:
I am trying to build on the help I've received from SO members when refactoring PySpark code to snowflake.
Can someone take a look at the following snowpark python code and let me know why I'm getting invalid syntax error:
import snowflake.snowpark as snowpark
from snowflake.snowpark.functions import col
def registerDelta(session: snowpark.Session,regName, stage, filePath):
pos = regName.index(".")
if pos > 0:
dbName = stage + regName[0:pos]
else:
dbName = stage
try:
crtdb = f"CREATE DATABASE IF NOT EXISTS {dbName}"
session.sql(crtdb)
print(f"{stage} - Database created")
except:
print("Create Database Failed")
try:
SQL = f"CREATE TABLE IF NOT EXISTS {stage}{regName} USING DELTA LOCATION '{filePath}'"
session.sql(SQL)
print(f"{stage}{regName} - Table created")
except:
print("Create Table Failed")
def main(session: snowpark.Session):
return registerDelta(session, 'test1', 'test2', 'test3')
I've got feeling this will highlight my lack of Python coding knowledge.
Anyway, any thoughts?
答案1
得分: 1
This issue is purely based on code and it is not specific to Snowpark.
pos = regName.index(".") # 当字符串未找到时,会引发错误
if pos > 0:
dbName = stage + regName[0:pos]
else:
dbName = stage
> 我得到了无效的语法错误:
>
> ValueError: 未找到子字符串
这个错误不是语法错误,而是由于提供的输入引起的运行时错误,这是正确的行为。
Python str.index()
> str.index(sub[, start[, end]])
>
> 类似于find(),但当未找到子字符串时引发ValueError错误。
Python str.find()
>
> str.find(sub[, start[, end]])
>
> 返回字符串中子字符串sub第一次出现的最低索引,该子字符串位于s[start:end]的切片内。可选参数start和end的解释与切片表示法中的相同。如果未找到子字符串sub,则返回-1。
英文:
This issue is purely based on code and it is not specific to Snowpark.
pos = regName.index(".") # when string is not found it raises an error
if pos > 0:
dbName = stage + regName[0:pos]
else:
dbName = stage
> I'm getting invalid syntax error:
>
> ValueError: substring not found
This error is not a syntax error but a runtime error due to provided input and it is correct behavior.
Python str.index()
> str.index(sub[, start[, end]])
>
> Like find(), but raise ValueError when the substring is not found.
Python str.find()
>
> str.find(sub[, start[, end]])
>
> Return the lowest index in the string where substring sub is found within the slice s[start:end]. Optional arguments start and end are interpreted as in slice notation. Return -1 if sub is not found.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论