要求:编写一个程序,打开文件夹中所有的.txt 文件,查找匹配用户提供的正则表达式的所有行。结果应该打印到屏幕上。 以下是代码:
import os,re #找到所有txt文件 userPath = '.' fileList = os.listdir(userPath) userRegexRule = 'NOUN' userRegex = re.compile(userRegexRule) lines = [] #读取所有txt文件的内容 for i in fileList: #print(os.path.basename(__file__))也可获取当前文件名 if os.path.basename(i)[-3:] == 'txt': ifile = open(i) icnt = ifile.readlines() #readlines返回每行内容的字符串列表 ifile.close() #匹配txt文件的符合要求的每行 for num in range(len(icnt)): #遍历列表每一个 if userRegex.search(icnt[num]): print(str(icnt[num]).rstrip('\n')) #lines.append(icnt[num]) lines += list(icnt[num]) print(lines)运行结果如下:
The ADJECTIVE panda walked to the NOUN and then VERB. A nearby NOUN was unaffected by these events. The ADJECTIVE panda walked to the NOUN and then VERB. A nearby NOUN was unaffected by these events. ['T', 'h', 'e', ' ', 'A', 'D', 'J', 'E', 'C', 'T', 'I', 'V', 'E', ' ', 'p', 'a', 'n', 'd', 'a', ' ', 'w', 'a', 'l', 'k', 'e', 'd', ' ', 't', 'o', ' ', 't', 'h', 'e', ' ', 'N', 'O', 'U', 'N', ' ', 'a', 'n', 'd', ' ', 't', 'h', 'e', 'n', ' ', 'V', 'E', 'R', 'B', '.', ' ', 'A', ' ', 'n', 'e', 'a', 'r', 'b', 'y', ' ', 'N', 'O', 'U', 'N', ' ', 'w', 'a', 's', ' ', 'u', 'n', 'a', 'f', 'f', 'e', 'c', 't', 'e', 'd', ' ', 'b', 'y', ' ', 't', 'h', 'e', 's', 'e', ' ', 'e', 'v', 'e', 'n', 't', 's', '.', '\n', 'T', 'h', 'e', ' ', 'A', 'D', 'J', 'E', 'C', 'T', 'I', 'V', 'E', ' ', 'p', 'a', 'n', 'd', 'a', ' ', 'w', 'a', 'l', 'k', 'e', 'd', ' ', 't', 'o', ' ', 't', 'h', 'e', ' ', 'N', 'O', 'U', 'N', ' ', 'a', 'n', 'd', ' ', 't', 'h', 'e', 'n', ' ', 'V', 'E', 'R', 'B', '.', ' ', '\n', 'A', ' ', 'n', 'e', 'a', 'r', 'b', 'y', ' ', 'N', 'O', 'U', 'N', ' ', 'w', 'a', 's', ' ', 'u', 'n', 'a', 'f', 'f', 'e', 'c', 't', 'e', 'd', ' ', 'b', 'y', ' ', 't', 'h', 'e', 's', 'e', ' ', 'e', 'v', 'e', 'n', 't', 's', '.', '\n']line 并没有返回预期的结果,而是一个个的字符。哪里出了问题? 写一个测试脚本:
a = [1,2,'a'] b = 'hi' print(a + b)执行报错:
Traceback (most recent call last): File "test.py", line 3, in <module> print(a + b) TypeError: can only concatenate list (not "str") to liststr 不能与 list 相加,试试用list()函数进行转换。
a = [1,2,'a'] b = 'hi' print(a + list(b)) [1, 2, 'a', 'h', 'i']修改代码后执行结果如上,然后想起来字符串和列表之间就很有点猫腻。
字符串转列表用 split(),列表转字符串用join()。
a = [1,2,'a'] b = 'hi there 1 2 3' print(a + list(b)) print(list(b)) print(''.join(a)) print(b.split(' '))运行后报错:
Traceback (most recent call last): File "test.py", line 5, in <module> print(''.join(a)) TypeError: sequence item 0: expected str instance, int found当列表中有数字时,join()会报错。尝试用 str(a)转换后结果为
a = [1,2,'a'] print(''.join(str(a))) [1, 2, 'a']虽然没有报错,但是并没有转换成字符串12a,而是将列表本身作为字符串了,相当于"[1,2,'a']"
a = ['1','2','a'] b = 'hi there 1 2 3' print(a + list(b)) print(''.join(a)) print(list(b)) print(b.split(' ')) ['1', '2', 'a', 'h', 'i', ' ', 't', 'h', 'e', 'r', 'e', ' ', '1', ' ', '2', ' ', '3'] 12a ['h', 'i', ' ', 't', 'h', 'e', 'r', 'e', ' ', '1', ' ', '2', ' ', '3'] ['hi', 'there', '1', '2', '3']a + list(b)也是将字符串 b 转换成单个字符的列表再相加
字符串像列表一样也可以进行下标取值、切片、for循环、len()、in、not in等操作,它属于不可变对象。而列表属于可变对象。
>>> a=1 >>> b=a >>> a+=1 >>> a,b (2, 1) >>> a=[1,2,3,4] >>> b=a >>> a+=[5] >>> a,b ([1, 2, 3, 4, 5], [1, 2, 3, 4, 5])列表的赋值相当于将引用赋给变量。 举个例子: 现在某小区某栋1单元有5户,从101-105,101室人家有有个非常漂亮的打火机,102户想要借用一下,101户很高兴地同意了。101室就是变量名,变量所对应的值就是室内某个具体的物件。这个过程相当于一般变量间的赋值,当101室内发生变化时,比如101觉得不方便又买了一个打火机,但并不会影响到102室继续死皮赖脸的来借。 而列表相当于整个1单元。
需要导入copy模块,利用 copy.copy(list) 引用列表。 当列表里包含列表时,可以用copy.deepcopy(list)
参考:https://www.zhihu.com/question/20114936