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

时间:2018-12-24 08:21:38

标签: python regex

我想从下面的原始内容中获取名称(仅第一行)。你能帮我么?我只想使用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."

3 个答案:

答案 0 :(得分:4)

只需使用正则表达式即可:

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

输出:

RAM KUMAR

编辑:

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

答案 1 :(得分:3)

也许使用split做这样的事情:

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'

答案 2 :(得分:1)

由于该问题被标记为,因此我将为其提供基于正则表达式的解决方案。

使用以下表达式:

^[^\n]+

Demo

我所要做的就是从行首开始匹配'\n'字符以外的所有字符。

随后的比赛将是您感兴趣的结果。