如何将多行语句字符串放入变量中?

2024-10-02 04:17:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我想将一个长字符串(特别是SQL查询)存储到一个变量中,为了更好的可读性,我想在更多的行中写入该变量

如果这很重要的话,我正在Python3.5(Anaconda)上使用Jupyter笔记本

我试过:

# SQL query

query = "
SELECT
 Sued 
,ApplicationNumber_Primary
--,ApprovedLoanAmount, ApprovedLoanDuration, ApprovedMonthlyPayment, 
,RequiredLoanDuration, RequiredMonthlyPaymentAmount, RequiredPaidAmount, RequiredCoefficientK1
,ClientFreeSources, ClientTotalIncome, ClientNetIncome, ClientTotalExpenditures 
,ClientAgeToApplicationDate, ClientFamilyStatusID, ClientEmploymentDuration, CreditExposure
,CalendarQuarter, MonthOfYear, WeekOfYear, DayOfMonth, DayOfWeek, RegionID, DistrictID, ZIPcodeID 

FROM 
dbo.vRisk
GO
"

…它不会像我希望的那样将字符串存储到变量中

我们将不胜感激


Tags: 字符串sql笔记本jupyteranacondaqueryselect可读性
2条回答

尝试使用三重引号:

query = """
SELECT
 Sued 
,ApplicationNumber_Primary
 ,ApprovedLoanAmount, ApprovedLoanDuration, ApprovedMonthlyPayment, 
,RequiredLoanDuration, RequiredMonthlyPaymentAmount, RequiredPaidAmount, RequiredCoefficientK1
,ClientFreeSources, ClientTotalIncome, ClientNetIncome, ClientTotalExpenditures 
,ClientAgeToApplicationDate, ClientFamilyStatusID, ClientEmploymentDuration, CreditExposure
,CalendarQuarter, MonthOfYear, WeekOfYear, DayOfMonth, DayOfWeek, RegionID, DistrictID, ZIPcodeID 

FROM 
dbo.vRisk
GO
"""

使用多行字符串或在其中穿插“\n”:

this_is_a_multiline_string = """

tata

"""

this_as_well = '''

tata

'''

and_this = "\ntata\n"

Python.org string documentation


and_like_so = ("Some string"      # no space after
               "that spans lots"  # no space after
               "of lines" )       # results in 'Some stringthat spans lotsof lines'

(最后一个的来源:https://stackoverflow.com/a/10660443/7505395

相关问题 更多 >

    热门问题