SQL 服务器存储过程 Python 传递的文件路径

SQL Server stored procedure Python passed file path

我正在尝试将文件目录传递给 Python 脚本以处理内容。这是 EXEC 语句,我的代码下面是错误。它应该打开文件并进行一些处理。

EXEC ScrapeData'F:\FinancialResearch\SEC\myEdgar\sec-edgar-filings\WIRE-Q[=23=]01753926-20-000110\full-submission.txt'

DROP PROCEDURE IF EXISTS ScrapeData;
GO

CREATE PROCEDURE [dbo].[ScrapeData] 
    (@filepath varchar(300))
AS
BEGIN
    EXEC sp_execute_external_script
            @language = N'Python',
            @script = N'
import html2text
from bs4 import BeautifulSoup
import pyodbc
import re
import requests
import pandas as pd

with open(@filepath) as f:
        contents = f.read()
        soup = BeautifulSoup(contents, ''lxml'')
.....

错误:

Msg 39004, Level 16, State 20, Line 0
A 'Python' script error occurred during execution of 'sp_execute_external_script' with HRESULT 0x80004004.

Msg 39019, Level 16, State 2, Line 0
An external script error occurred:

Error in execution. Check the output for more information.

Traceback (most recent call last):
File "", line 3, in
File "E:\ProgramData\REAPER\Temp-PY\Appcontainer1\DE944528-CBDA-4AFD-B50F-32AD2CFD35B7\sqlindb_0.py", line 45
with open(@filepath) as f:
^
SyntaxError: invalid syntax

SqlSatelliteCall error: Error in execution. Check the output for more information.

STDOUT message(s) from external script:

SqlSatelliteCall 函数失败。请查看控制台输出以获取更多信息。

...对于文件 c:\temp\test.txt..

declare @myfile varchar(100) = 'c:\temp\test.txt';

EXEC sp_execute_external_script @language = N'Python', @script = N'
print(filename)
print(xyz)
print(paramZ)
num_lines = sum(1 for line in open(filename))
print("------")
print(num_lines)
',
@params= N'@filename varchar(100), @paramZ int, @xyz varchar(10)', @filename = @myfile, @xyz='abcd', @paramZ = 100;