Python中文
首页
教程
问答
标签
搜索
登录
注册
在Python正则表达式中使用^匹配行首
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我正试图从汤森路透科学网(Thomson Reuters Web of Science)中提取出出版年份的ISI风格数据。“出版年”的行如下所示(在行的开头):</p> <pre><code>PY 2015 </code></pre> <p>对于我正在编写的脚本,我定义了以下regex函数:</p> <pre><code>import re f = open('savedrecs.txt') wosrecords = f.read() def findyears(): result = re.findall(r'PY (\d\d\d\d)', wosrecords) print result findyears() </code></pre> <p>然而,这会产生假阳性结果,因为模式可能出现在数据的其他地方。</p> <p>所以,我只想匹配一行开头的模式。通常我会为此使用<code>^</code>,但是<code>r'^PY (\d\d\d\d)'</code>无法匹配我的结果。另一方面,使用<code>\n</code>似乎可以达到我的目的,但这可能会导致我的进一步复杂化。</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<pre><code>re.findall(r'^PY (\d\d\d\d)', wosrecords, flags=re.MULTILINE) </code></pre> <p>应该有用的,如果不行就告诉我。我没有你的数据。</p>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
VirtualEnvRapper错误:路径python2(来自python=python2)不存在
9 回答
virtualenvs上的pyinstaller,没有名为导入错误的模块
9 回答
virtualenvs是否可以退回到用户包而不是系统包?
1 回答
virtualenvwrapper CentOS7
10 回答
virtualenvwrapper IOError:[Errno 13]权限被拒绝
4 回答
virtualenvwrapper mkproject和shell在windows中的启动问题?
9 回答
virtualenvwrapper mkvirtualenv不工作但没有错误
4 回答
Virtualenvwrapper python bash
4 回答
virtualenvwrapper:“workon”何时更改到项目目录?
3 回答
virtualenvwrapper:mkvirtualenv可以工作,但是rmvirtualenv返回bash:没有这样的文件或目录
7 回答
virtualenvwrapper:virtualenv信息存储在哪里?
2 回答
virtualenvwrapper:命令“python设置.pyegg_info“失败,错误代码为1
4 回答
virtualenvwrapper:如何将mkvirtualenv的默认Python版本/路径更改为ins
3 回答
Virtualenvwrapper:模块“pkg_resources”没有属性“iter_entry_points”
2 回答
Virtualenvwrapper:没有名为virtualenvwrapp的模块
10 回答
Virtualenvwrapper.bash_profi的正确设置
1 回答
Virtualenvwrapper.hook:权限被拒绝
8 回答
virtualenvwrapper.sh:fork:资源暂时不可用Python/Djang
6 回答
Virtualenvwrapper.shlssitepackages命令不工作
5 回答
Virtualenvwrapper.sh函数在bash sh中不可用
10 回答