#python #string #list #syntax #syntax-error
Вопрос:
У меня есть длинный список слов, которые я хочу просмотреть:
company_name = [Adrian Lee amp; Partners, Aegon Asset Management, Amundi Asset Management, Australiansuper, Aviva Investors, AXA Investment Management, Bank of America, Bank of England,Banque de Luxembourg,Barclays,Berenberg,Bierbaum Pro ag,BlackRock,BlondeMoney,BlueBay Asset Management,Bluecrest Capital Management,Brevan Howard,China Construction Bank Corporation,Credit Suisse,Credit Suisse Asset Managemenet,Cuemacro,Deutsche Boerse,Dimensional Fund Advisors,Divento Financials limited,Eaton Vance,Edmond De Rothschild Asset management gmbh,Erste Asset Management (EAM),Eurex,ExoAlpha,Fidelity Investments,Financial Times,Findlay Park partners llp,Florin Court Capital LLP,GAM investments,GIC Capital Corp,GSA Capital,H2O AM LLP,ING Bank NV, London Branch,Invesco,International Standard Asset Management,J.P. Morgan,Janus Henderson,JPMorgan Asset Management,Jupiter Asset Management,LCH,Legal amp; General Investment Management,LMRKTS LLC,Lombard Odier Investment Managers,Man Group,Manulife Investment Management,Metzler Asset Management ,Millennium Global Investments,Morgan Stanley,Morgan Stanley Investment Management,Moscow Exchange,Ninety One,Nomura Asset Management,Norges Bank Investment Management,Optiver,Payden amp; Rygel,Pictet Asset Management,Pimco,Principal Global Investors,Putnam Investments,Record Currency Management,Renaissance Capital,Rokos Capital Management,Russell Investments,Schroders,State Street,Systematica Investments,T. Rowe Price,Tellurian Capital Management llp,Thalesians ltd,UBS,UBS Asset Management,Union Investment institutional,United National Bank,USS Investment Management,Wellington Management,White Oak Asset Management,Winton,Zürcher Kantonalbank,VZ Depotbank AG,eToro,Daiwa Capital Markets,VTB Capital,Sberbank,BNY Mellon | Pershing,Livi Bank,Wells Fargo Securities,Bank Leumi,RedBurn,VTB Capital,MUFG,ICAP,Moore Capital,Mizuho Bank,Prescient,RBC Capital Markets,Peel Hunt,KBC Secutiries,Swedbank,Sparebank1,Man Group,Jefferies,Nedbank,Avior Capital Markets (pty) ltd,Avalon Capital Markets,Neveus,Forte Securities limited,BOCI,Zeus Capital,BMO Capital Markets,China Merchants Securities co. ltd,IG Group,Handelsbanken,TD Securities,BNP Paribas,Unicredit,mitsubishi ufj lease amp; finance co. ltd.,ING Bank,nordea markets, corporates amp; institutions,Commonwealth Bank of Australia,Danske Bank,Wiener Privatbank se,ABSA Bank SA,HSBC,Canaccord Genuity- Global Capital Markets,Dowgate Capital Limited,Trium Capital,BTIG,Sova Capital,Saxo Bank,Cantor fitzgerald,FinnCap group,BBVA,M.M.Warburg amp; CO,Bank of Tokyo UFG,lseg (London Stock Exchange Group),Kotak securities,CF Global Trading,Liberum Capital Participações Ltda,Rand Merchant Bank,Cathay United Bank,Steubing ag,Credit Agricole cib,Arden Partners,Banco de Investimento Global (BIG),BMO Capital Markets,Panmure Gordon amp; co,Santander bank,Deutsche Bank,Mizuho Securities,Numis,Bainbridge partners llp,Jutlander Bank,Cirdan Capital,MWB FairTrade ag,Rezco Asset Management,Shore Capital markets,Hobart Capital Markets llp,Baader Bank ag,Linear Investment ltd,Arion Banki,Kotak Securities]
Я получаю синтаксическую ошибку, которая, как я полагаю, является результатом того, что слова не заключены в кавычки. Если это так, есть ли способ закодировать его, чтобы поместить их все в кавычки, или мне нужно сделать это вручную? Если это не причина синтаксической ошибки, может ли кто-нибудь увидеть, что здесь не так?
Спасибо!
Комментарии:
1. Ваша ошибка действительно заключается в том, что вы пропускаете кавычки вокруг каждой строки в списке. В качестве альтернативы, превратите все это в одну строку, а затем разделите ее на запятую:
commany_names = "Adrian Lee amp; Partners, Aegon Asset Management, ...".split(",")
Ответ №1:
Всякий раз, когда у меня возникает одна из этих проблем, это помогает запустить Sublime Text и быстро найти замену.
В этом случае вы захотите, чтобы ваши данные выглядели так, чтобы Python мог их прочитать:
company_names = ['Adrian Lee amp; Partners','Aegon Asset Management','Amundi Asset Management','Australiansuper','Aviva Investors','AXA Investment Management','Bank of America','Bank of England','Banque de Luxembourg','Barclays','Berenberg','Bierbaum Pro ag','BlackRock','BlondeMoney','BlueBay Asset Management','Bluecrest Capital Management','Brevan Howard','China Construction Bank Corporation','Credit Suisse','Credit Suisse Asset Managemenet','Cuemacro','Deutsche Boerse','Dimensional Fund Advisors','Divento Financials limited','Eaton Vance','Edmond De Rothschild Asset management gmbh','Erste Asset Management (EAM)','Eurex','ExoAlpha','Fidelity Investments','Financial Times','Findlay Park partners llp','Florin Court Capital LLP','GAM investments','GIC Capital Corp','GSA Capital','H2O AM LLP','ING Bank NV',' London Branch','Invesco','International Standard Asset Management','J.P. Morgan','Janus Henderson','JPMorgan Asset Management','Jupiter Asset Management','LCH','Legal amp; General Investment Management','LMRKTS LLC','Lombard Odier Investment Managers','Man Group','Manulife Investment Management','Metzler Asset Management ','Millennium Global Investments','Morgan Stanley','Morgan Stanley Investment Management','Moscow Exchange','Ninety One','Nomura Asset Management','Norges Bank Investment Management','Optiver','Payden amp; Rygel','Pictet Asset Management','Pimco','Principal Global Investors','Putnam Investments','Record Currency Management','Renaissance Capital','Rokos Capital Management','Russell Investments','Schroders','State Street','Systematica Investments','T. Rowe Price','Tellurian Capital Management llp','Thalesians ltd','UBS','UBS Asset Management','Union Investment institutional','United National Bank','USS Investment Management','Wellington Management','White Oak Asset Management','Winton','Zürcher Kantonalbank','VZ Depotbank AG','eToro','Daiwa Capital Markets','VTB Capital','Sberbank','BNY Mellon | Pershing','Livi Bank','Wells Fargo Securities','Bank Leumi','RedBurn','VTB Capital','MUFG','ICAP','Moore Capital','Mizuho Bank','Prescient','RBC Capital Markets','Peel Hunt','KBC Secutiries','Swedbank','Sparebank1','Man Group','Jefferies','Nedbank','Avior Capital Markets (pty) ltd','Avalon Capital Markets','Neveus','Forte Securities limited','BOCI','Zeus Capital','BMO Capital Markets','China Merchants Securities co. ltd','IG Group','Handelsbanken','TD Securities','BNP Paribas','Unicredit','mitsubishi ufj lease amp; finance co. ltd.','ING Bank','nordea markets','corporates amp; institutions','Commonwealth Bank of Australia','Danske Bank','Wiener Privatbank se','ABSA Bank SA','HSBC','Canaccord Genuity- Global Capital Markets','Dowgate Capital Limited','Trium Capital','BTIG','Sova Capital','Saxo Bank','Cantor fitzgerald','FinnCap group','BBVA','M.M.Warburg amp; CO','Bank of Tokyo UFG','lseg (London Stock Exchange Group)','Kotak securities','CF Global Trading','Liberum Capital Participações Ltda','Rand Merchant Bank','Cathay United Bank','Steubing ag','Credit Agricole cib','Arden Partners','Banco de Investimento Global (BIG)','BMO Capital Markets','Panmure Gordon amp; co','Santander bank','Deutsche Bank','Mizuho Securities','Numis','Bainbridge partners llp','Jutlander Bank','Cirdan Capital','MWB FairTrade ag','Rezco Asset Management','Shore Capital markets','Hobart Capital Markets llp','Baader Bank ag','Linear Investment ltd','Arion Banki','Kotak Securities']
Ответ №2:
Очевидно, проблема в том, что все строки не заключены в кавычки.
company_name = "Adrian Lee amp; Partners, Aegon Asset Management, Amundi Asset Management, Australiansuper, Aviva Investors, AXA Investment Management, Bank of America, Bank of England,Banque de Luxembourg,Barclays,Berenberg,Bierbaum Pro ag,BlackRock,BlondeMoney,BlueBay Asset Management,Bluecrest Capital Management,Brevan Howard,China Construction Bank Corporation,Credit Suisse,Credit Suisse Asset Managemenet,Cuemacro,Deutsche Boerse,Dimensional Fund Advisors,Divento Financials limited,Eaton Vance,Edmond De Rothschild Asset management gmbh,Erste Asset Management (EAM),Eurex,ExoAlpha,Fidelity Investments,Financial Times,Findlay Park partners llp,Florin Court Capital LLP,GAM investments,GIC Capital Corp,GSA Capital,H2O AM LLP,ING Bank NV, London Branch,Invesco,International Standard Asset Management,J.P. Morgan,Janus Henderson,JPMorgan Asset Management,Jupiter Asset Management,LCH,Legal amp; General Investment Management,LMRKTS LLC,Lombard Odier Investment Managers,Man Group,Manulife Investment Management,Metzler Asset Management ,Millennium Global Investments,Morgan Stanley,Morgan Stanley Investment Management,Moscow Exchange,Ninety One,Nomura Asset Management,Norges Bank Investment Management,Optiver,Payden amp; Rygel,Pictet Asset Management,Pimco,Principal Global Investors,Putnam Investments,Record Currency Management,Renaissance Capital,Rokos Capital Management,Russell Investments,Schroders,State Street,Systematica Investments,T. Rowe Price,Tellurian Capital Management llp,Thalesians ltd,UBS,UBS Asset Management,Union Investment institutional,United National Bank,USS Investment Management,Wellington Management,White Oak Asset Management,Winton,Zürcher Kantonalbank,VZ Depotbank AG,eToro,Daiwa Capital Markets,VTB Capital,Sberbank,BNY Mellon | Pershing,Livi Bank,Wells Fargo Securities,Bank Leumi,RedBurn,VTB Capital,MUFG,ICAP,Moore Capital,Mizuho Bank,Prescient,RBC Capital Markets,Peel Hunt,KBC Secutiries,Swedbank,Sparebank1,Man Group,Jefferies,Nedbank,Avior Capital Markets (pty) ltd,Avalon Capital Markets,Neveus,Forte Securities limited,BOCI,Zeus Capital,BMO Capital Markets,China Merchants Securities co. ltd,IG Group,Handelsbanken,TD Securities,BNP Paribas,Unicredit,mitsubishi ufj lease amp; finance co. ltd.,ING Bank,nordea markets, corporates amp; institutions,Commonwealth Bank of Australia,Danske Bank,Wiener Privatbank se,ABSA Bank SA,HSBC,Canaccord Genuity- Global Capital Markets,Dowgate Capital Limited,Trium Capital,BTIG,Sova Capital,Saxo Bank,Cantor fitzgerald,FinnCap group,BBVA,M.M.Warburg amp; CO,Bank of Tokyo UFG,lseg (London Stock Exchange Group),Kotak securities,CF Global Trading,Liberum Capital Participações Ltda,Rand Merchant Bank,Cathay United Bank,Steubing ag,Credit Agricole cib,Arden Partners,Banco de Investimento Global (BIG),BMO Capital Markets,Panmure Gordon amp; co,Santander bank,Deutsche Bank,Mizuho Securities,Numis,Bainbridge partners llp,Jutlander Bank,Cirdan Capital,MWB FairTrade ag,Rezco Asset Management,Shore Capital markets,Hobart Capital Markets llp,Baader Bank ag,Linear Investment ltd,Arion Banki,Kotak Securities".split(", ")
Ответ №3:
просто поставьте кавычки после [
и до ]
этого, после того, как кавычки будут записаны .разделите(«,») и удалите эти квадратные скобки, такие как:
company_name = "Adrian Lee amp; Partners, Aegon Asset Management, Amundi Asset Management, Australiansuper, Aviva Investors, AXA Investment Management, Bank of America, Bank of England,Banque de Luxembourg,Barclays,Berenberg,Bierbaum Pro ag,BlackRock,BlondeMoney,BlueBay Asset Management,Bluecrest Capital Management,Brevan Howard,China Construction Bank Corporation,Credit Suisse,Credit Suisse Asset Managemenet,Cuemacro,Deutsche Boerse,Dimensional Fund Advisors,Divento Financials limited,Eaton Vance,Edmond De Rothschild Asset management gmbh,Erste Asset Management (EAM),Eurex,ExoAlpha,Fidelity Investments,Financial Times,Findlay Park partners llp,Florin Court Capital LLP,GAM investments,GIC Capital Corp,GSA Capital,H2O AM LLP,ING Bank NV, London Branch,Invesco,International Standard Asset Management,J.P. Morgan,Janus Henderson,JPMorgan Asset Management,Jupiter Asset Management,LCH,Legal amp; General Investment Management,LMRKTS LLC,Lombard Odier Investment Managers,Man Group,Manulife Investment Management,Metzler Asset Management ,Millennium Global Investments,Morgan Stanley,Morgan Stanley Investment Management,Moscow Exchange,Ninety One,Nomura Asset Management,Norges Bank Investment Management,Optiver,Payden amp; Rygel,Pictet Asset Management,Pimco,Principal Global Investors,Putnam Investments,Record Currency Management,Renaissance Capital,Rokos Capital Management,Russell Investments,Schroders,State Street,Systematica Investments,T. Rowe Price,Tellurian Capital Management llp,Thalesians ltd,UBS,UBS Asset Management,Union Investment institutional,United National Bank,USS Investment Management,Wellington Management,White Oak Asset Management,Winton,Zürcher Kantonalbank,VZ Depotbank AG,eToro,Daiwa Capital Markets,VTB Capital,Sberbank,BNY Mellon | Pershing,Livi Bank,Wells Fargo Securities,Bank Leumi,RedBurn,VTB Capital,MUFG,ICAP,Moore Capital,Mizuho Bank,Prescient,RBC Capital Markets,Peel Hunt,KBC Secutiries,Swedbank,Sparebank1,Man Group,Jefferies,Nedbank,Avior Capital Markets (pty) ltd,Avalon Capital Markets,Neveus,Forte Securities limited,BOCI,Zeus Capital,BMO Capital Markets,China Merchants Securities co. ltd,IG Group,Handelsbanken,TD Securities,BNP Paribas,Unicredit,mitsubishi ufj lease amp; finance co. ltd.,ING Bank,nordea markets, corporates amp; institutions,Commonwealth Bank of Australia,Danske Bank,Wiener Privatbank se,ABSA Bank SA,HSBC,Canaccord Genuity- Global Capital Markets,Dowgate Capital Limited,Trium Capital,BTIG,Sova Capital,Saxo Bank,Cantor fitzgerald,FinnCap group,BBVA,M.M.Warburg amp; CO,Bank of Tokyo UFG,lseg (London Stock Exchange Group),Kotak securities,CF Global Trading,Liberum Capital Participações Ltda,Rand Merchant Bank,Cathay United Bank,Steubing ag,Credit Agricole cib,Arden Partners,Banco de Investimento Global (BIG),BMO Capital Markets,Panmure Gordon amp; co,Santander bank,Deutsche Bank,Mizuho Securities,Numis,Bainbridge partners llp,Jutlander Bank,Cirdan Capital,MWB FairTrade ag,Rezco Asset Management,Shore Capital markets,Hobart Capital Markets llp,Baader Bank ag,Linear Investment ltd,Arion Banki,Kotak Securities".split(", ")
это даст вам список названия компании в переменной company_name
чтобы извлечь слова из этих названий
words = [word for word in name.split() for name in company_name]
и вот ты здесь 😉