# 用open()读入文本
f = open(r'C:\Users\HP\Desktop\金融数据获取与处理\final homework\homework\homework1\作业一素材.txt',encoding='utf-8')
txt = f.read()#读取文件
f.close()
txt
'010\n2019年度报告\u2002AnnuAl report\nIX. Non-Recurring Profit or Loss\n√ Applicable Not applicable\nUnit: RMB\n2018 2017 \nItem 2019 Note\n(Restated) (Restated)\nFor details, please refer to \nInvestment income from disposal of subsidiaries 848,211,045.94 807,518,533.61 – Note 5(3)(a) of Chapter 12 \nFinancial Report.\nGains on disposals of non-current assets (including \noffsetting amount for the provision of impairment of 31,356,737.19 71,160,937.90 1,075,903,723.03\nassets)\nIt mainly represents \nGovernment grants recognized in profit or loss for government subsidies \nthe current period (excluding government grants arising from fiscal \nthat are closely related to the Company’s business 545,508,216.82 295,443,847.50 230,922,102.34 appropriation, \noperations, in accordance with national uniform transportation capacity \nstandards) subsidies and tax refund, \netc.\nNet loss/profit arising from business combinations \ninvolving enterprises under common control from the \n-2,123,517.28 -43,047,655.76 3,495,095.51\nbeginning of reporting period to the combination \ndate\nGains or losses from changes in fair value of financial \nassets and liabilities held for trading, derivative \nfinancial assets and liabilities, and investment (losses)/ It mainly attributable to the \ngains arising from the disposal of financial assets and gains arising from changes \n364,072,933.26 -8,398,916.11 14,060,964.04\nliabilities held for trading, derivative financial assets in fair value of other non-\nand liabilities and other debt investments, excluding current financial assets.\nhedging activities related to the normal business \noperations of the Company\nIncome from external entrusted loans – 416,510.40 5,060,742.88\nNet amount of other non-operating income and \n-38,285,034.94 13,515,766.49 24,976,445.24\nexpenses\nLess: Income tax effect 159,282,664.33 63,672,015.52 281,823,573.43\n Profit or loss attributable to minority \n716,085.28 620,414.11 1,048,197.18\n shareholders (after tax)\nNon-recurring profit or loss attributable to \n1,588,741,631.38 1,072,316,594.40 1,071,547,302.43 –\nshareholders of the parent company\nProvide explanations for classifying non-recurring profit and loss items defined in the Explanatory Announcement No. 1 for Public Company Information \nDisclosures – Non-recurring Profits and Losses, and for classifying non-recurring profit and loss items listed in the Explanatory Announcement No. 1 for \nPublic Company Information Disclosures – Non-recurring Profits and Losses as recurring profit and loss items.\n Applicable √ Not applicable\nThe Company has not classified non-recurring profit and loss items defined or listed in the Explanatory Announcement No. 1 for Public Company \nInformation Disclosures – Non-recurring Profits and Losses in the reporting period.\n顺丰控股股份有限公司\nS.F. HOLDING CO., LTD.'
index = txt.find('\n顺丰控股股份有限公司')#定位页脚位置
txt_target = txt[:index]#剔除页脚
txt_target
'010\n2019年度报告\u2002AnnuAl report\nIX. Non-Recurring Profit or Loss\n√ Applicable Not applicable\nUnit: RMB\n2018 2017 \nItem 2019 Note\n(Restated) (Restated)\nFor details, please refer to \nInvestment income from disposal of subsidiaries 848,211,045.94 807,518,533.61 – Note 5(3)(a) of Chapter 12 \nFinancial Report.\nGains on disposals of non-current assets (including \noffsetting amount for the provision of impairment of 31,356,737.19 71,160,937.90 1,075,903,723.03\nassets)\nIt mainly represents \nGovernment grants recognized in profit or loss for government subsidies \nthe current period (excluding government grants arising from fiscal \nthat are closely related to the Company’s business 545,508,216.82 295,443,847.50 230,922,102.34 appropriation, \noperations, in accordance with national uniform transportation capacity \nstandards) subsidies and tax refund, \netc.\nNet loss/profit arising from business combinations \ninvolving enterprises under common control from the \n-2,123,517.28 -43,047,655.76 3,495,095.51\nbeginning of reporting period to the combination \ndate\nGains or losses from changes in fair value of financial \nassets and liabilities held for trading, derivative \nfinancial assets and liabilities, and investment (losses)/ It mainly attributable to the \ngains arising from the disposal of financial assets and gains arising from changes \n364,072,933.26 -8,398,916.11 14,060,964.04\nliabilities held for trading, derivative financial assets in fair value of other non-\nand liabilities and other debt investments, excluding current financial assets.\nhedging activities related to the normal business \noperations of the Company\nIncome from external entrusted loans – 416,510.40 5,060,742.88\nNet amount of other non-operating income and \n-38,285,034.94 13,515,766.49 24,976,445.24\nexpenses\nLess: Income tax effect 159,282,664.33 63,672,015.52 281,823,573.43\n Profit or loss attributable to minority \n716,085.28 620,414.11 1,048,197.18\n shareholders (after tax)\nNon-recurring profit or loss attributable to \n1,588,741,631.38 1,072,316,594.40 1,071,547,302.43 –\nshareholders of the parent company\nProvide explanations for classifying non-recurring profit and loss items defined in the Explanatory Announcement No. 1 for Public Company Information \nDisclosures – Non-recurring Profits and Losses, and for classifying non-recurring profit and loss items listed in the Explanatory Announcement No. 1 for \nPublic Company Information Disclosures – Non-recurring Profits and Losses as recurring profit and loss items.\n Applicable √ Not applicable\nThe Company has not classified non-recurring profit and loss items defined or listed in the Explanatory Announcement No. 1 for Public Company \nInformation Disclosures – Non-recurring Profits and Losses in the reporting period.'
# 通过最后句号和空格,界定最后一个单词
e = txt_target.rfind('.')#定位最后一个句号
s = txt_target.rfind(' ')#定位最后一个空格
last_word = txt_target[s+1:e]#提取最后一个单词
last_word
'period'
# 最后一个句子的长度
print(len(last_word))
6
#另一种方案
f= open(r'C:\Users\HP\Desktop\金融数据获取与处理\final homework\homework\homework1\作业一素材.txt',encoding='utf-8')
txt = f.read()
f.close()
index = txt.find('\n顺丰控股股份有限公司')
txt_target = txt[:index]
lines = txt_target.split('\n')
last_line = lines[-1]
words = last_line.split(' ')
last_word = words[-1]
for i in range(1,len(last_word)):
last_character = last_word[-i]
if last_character.isalpha():
last_word = last_word[:-i+1]
break
if i == len(last_word):
last_word = words[-2]
print(last_word)
# 输出最后一个单词长度
print(len(last_word))
period 6