使用python从原始文本获取第一行

2024-09-29 19:19:03 发布

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

我想从下面的原始内容中获取名称(仅第一行)。你能帮帮我吗?我只想使用python从原始文本中获取RAM KUMAR。你知道吗

原始内容:

"RAM KUMAR\n\nMarketing and Sales Professional\n\n+91.0000000000\n\nshri.babuji@shriresume.com, shri1.babuji@shriresume.com\n\nLinkedin.com/in/ramkumar              \t\t\t\t                           \n\n\t\t\t\n\n      \t                                                                                   \n\nSUMMARY\n\n\n\nHighly motivated, creative and versatile IT professional with 9.2 years of experience in Java, J2SE & J2EE and related technologies as Developer, Onsite/Offshore Coordinator and Project Lead.\n\nProficiency in Java, Servlets, Struts and the latest frameworks like JSF, EJB 3.0.\n\nKnowledge of Java, JSP, Servlet, EJB, JMS, Struts and spring, Hibernate, XML, Web Services.\n\nExperience in using MVC design pattern, Java, Servlets, JSP, JavaScript, Hibernate 3.0, Web Services (SOAP and Restful), HTML, JQuery, XML, Web Logic, JBOSS 4.2.3, SQL, PL/SQL, JUnit, and Apache-Tomcat, Linux.\n\nExtensive experience in developing various web based applications using Struts framework.\n\nExpertise in relational databases like Oracle, My SQL and SQL Server.\n\nExperienced in developing Web Based applications using Web Sphere 6.0 and Oracle 9i as a back end."

Tags: andofincomweb内容sqljava
3条回答

无需使用regex,只需执行以下操作:

print(yourstring.split('\n')[0])

输出:

RAM KUMAR

编辑:

with open(filename,'r') as f:
    print(f.read().split('\n')[0])

因为问题被标记为,所以我提供了一个基于regex的解决方案。你知道吗

使用以下表达式:

^[^\n]+

Demo

我所做的只是匹配行开头的'\n'字符以外的所有字符。你知道吗

第一场比赛就是你感兴趣的结果。你知道吗

使用^{}可以执行以下操作:

txt_content = "RAM KUMAR\n\nMarketing and Sales Professional\n\n+91.0000000000\n\nshri.babuji@shriresume.com, shri1.babuji@shriresume.com\n\nLinkedin.com/in/ramkumar              \t\t\t\t                           \n\n\t\t\t\n\n      \t                                                                                   \n\nSUMMARY\n\n\n\nHighly motivated, creative and versatile IT professional with 9.2 years of experience in Java, J2SE & J2EE and related technologies as Developer, Onsite/Offshore Coordinator and Project Lead.\n\nProficiency in Java, Servlets, Struts and the latest frameworks like JSF, EJB 3.0.\n\nKnowledge of Java, JSP, Servlet, EJB, JMS, Struts and spring, Hibernate, XML, Web Services.\n\nExperience in using MVC design pattern, Java, Servlets, JSP, JavaScript, Hibernate 3.0, Web Services (SOAP and Restful), HTML, JQuery, XML, Web Logic, JBOSS 4.2.3, SQL, PL/SQL, JUnit, and Apache-Tomcat, Linux.\n\nExtensive experience in developing various web based applications using Struts framework.\n\nExpertise in relational databases like Oracle, My SQL and SQL Server.\n\nExperienced in developing Web Based applications using Web Sphere 6.0 and Oracle 9i as a back end."
print(txt_content.split('\n')[0])    # 'RAM KUMAR'

相关问题 更多 >

    热门问题