简体   繁体   English

Python中的正则表达式,用于匹配CSV文件中的字符串

[英]Regular expression in Python for matching strings in a CSV file

I am working with regular expressions in Python. 我正在使用Python中的正则表达式。 I want to match a few lines from a CSV file inserted into a database that starts and ends with an underscore. 我想匹配插入到以下划线开头和结尾的数据库中的CSV文件中的几行。

I have used regular expressions in my Python script to do the same but it prints the result as 'none'. 我在Python脚本中使用了正则表达式来执行相同的操作,但是它将结果打印为“ none”。 Here is my code for the same, kindly tell me what mistake I am making: 这是我的代码,请告诉我我犯了什么错误:

reg = re.compile(r'^_.*_$',re.I)
imatch = reg.match(unicode(row[4], "utf8"))

Here r'^_.*_$',re.I is my regular expression to match lines starting and ending with _. 在这里r'^_.*_$',re.I是我的正则表达式,用于匹配以_开头和结尾的行。 unicode(row[4], "utf8") specifies the row from the CSV file inserted into a database. unicode(row[4], "utf8")指定插入到数据库中的CSV文件中的行。

Any help would be appreciated. 任何帮助,将不胜感激。

import re
lines = [line.strip() for line in open('file.csv')]
for x in lines:
    match=re.search(r'^_.*_$',x)
    if match: print x

we have to strip each line otherwise each line ends with char '\\n' instead of '_' in that case regex won't match the string. 我们必须删除每行,否则每行以char'\\ n'而不是'_结尾,在这种情况下,正则表达式将不匹配字符串。

file.csv file.csv

_abdlfla_
sldjlfds_
_adlfdls
_132jdlfjflds_

output 输出

_abdlfla_
_132jdlfjflds_

You may use startswith and endswith function instead of re. 您可以使用startswith和endswith函数代替re。 Any specific reason for using re? 使用re的任何特定原因?

for l in open('test.csv'):
    l=l.strip()
    if l.startswith('_') and l.endswith('_'):
        print(l)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM