【发布时间】:2020-07-05 15:20:30
【问题描述】:
# This module takes path of any random dataset from the user
class User_Input():
# Taking File Path as input from the user
user_input = input("Enter the path of your file: ")
# The assert keyword lets you test if a condition in your code returns True, if not, the program will raise an AssertionError.
assert os.path.exists(user_input), " did not find the file at, "+str(user_input)
user_file = open(user_input,'r+')
print(" found the file !")
for fp in user_file:
# Split the extension from the path and normalise it to lowercase.
ext = os.path.splitext(fp)[-1].lower().suffix()
# For handling breakdown in case of any errors
try:
# Now we can simply use .endswith to check for equality, no need for wildcards.
# For handling csv files
if ext.endswith('.csv'):
df = pd.read_csv(user_input)
# For handling excel files
elif ext.endswith('.xlsx'):
df = pd.read_excel(user_input)
# For handling json files
elif ext.endswith('.json'):
df = pd.read_json(user_input)
# For handing unknown files types
else:
print( fp,"is an unknown file format.")
break
# to handle the situation if try block gets failed
except:
pass
user_file.close()
我正在尝试自动上传任何文件扩展名(如 .csv、.json、.xlsx 等)并读取数据框中的代码。但是,这显示了断言错误,但我提供了正确的路径?编码也有问题!
它不能中断并告诉用户任何错误!
【问题讨论】: