从列表中删除 `\n`

Moh*_*han 1 python beautifulsoup web-scraping python-3.x

我有一个列表,其中包含从在线网站上抓取的数据。名单是这样的

list1 = ['\nJob Description\n\nDESCRIPTION: Interacts with users and technical team members to analyze requirements and develop
technical design specifications.  Troubleshoot complex issues and make recommendations to improve efficiency and accurac
y. Interpret complex data, analyze results using statistical techniques and provide ongoing reports. Identify, analyze,
and interpret trends or patterns in complex data sets. Filter and "clean data, review reports, and performance indicator
s to locate and correct code problems. Work closely with management to prioritize business and information needs. Locate
 and define new process improvement opportunities. Employ excellent interpersonal and verbal communication skills necess
ary to effectively coordinate interrelated activities with coworkers, end-users, and management. Works autonomously with
 minimal supervision. Provides technical guidance and mentoring to other team members. Multi tasks and balances multiple
 assignments and priorities. Provides timely status updates.\nQUALIFICATIONS: Proven 5 years working experience as a dat
a analyst Technical expertise regarding data models, database design development, data mining and segmentation technique
s Knowledge of and experience with reporting packages (preferably Microsoft BI Stack), databases (SQL, DB2 etc.), and qu
ery language (SQL) Knowledge of statistics and experience using statistical packages for analyzing large datasets Strong
 analytical skills with the ability to collect, organize, analyze, and disseminate significant amounts of information wi
th attention to detail and accuracy Adept at queries, report writing and presenting findings\nNTT DATA is a leading IT s.............]
Run Code Online (Sandbox Code Playgroud)

如何删除“\n”

请记住,它必须在抓取时在循环中完成,以便抓取数据,“\n”和不需要的空格被删除,并将数据推送到 csv 中。

jat*_*h03 5

尝试这个:

list2 = [x.replace('\n', '') for x in list1]
Run Code Online (Sandbox Code Playgroud)

它使用列表理解来迭代list1并从原始成员中创建一个新列表,并str.replace在每个项目上调用以将 替换为\n空字符串。

更多关于这里的python列表理解。

要删除空格,请将上面的代码更改为

list2 = [x.replace('\n', '').replace(' ', '') for x in list1]
Run Code Online (Sandbox Code Playgroud)