python,规定字符串malloc与字符串free相匹配,在a.csv的第10列中遍历所有与字符串malloc相匹配的字符串free,如果字符串malloc所在行的后两列与字符串free所在行的后两列的字符串相同,输出yes,反之输出No
时间: 2023-05-20 19:02:09 浏览: 59
可以使用Python的csv模块来读取a.csv文件,并使用正则表达式匹配字符串malloc和字符串free。然后,可以使用循环遍历所有匹配的字符串malloc和字符串free,并比较它们所在行的后两列的字符串是否相同。如果相同,输出yes,否则输出No。
以下是示例代码:
```python
import csv
import re
# 正则表达式匹配字符串malloc和字符串free
malloc_pattern = re.compile(r'\bmalloc\b')
free_pattern = re.compile(r'\bfree\b')
# 打开a.csv文件
with open('a.csv', newline='') as csvfile:
reader = csv.reader(csvfile)
for row in reader:
# 在第10列中查找匹配的字符串malloc和字符串free
for i, cell in enumerate(row):
if malloc_pattern.search(cell):
malloc_row = row
malloc_col = i
elif free_pattern.search(cell):
free_row = row
free_col = i
# 如果找到了匹配的字符串malloc和字符串free
if 'malloc_row' in locals() and 'free_row' in locals():
# 比较它们所在行的后两列的字符串是否相同
if malloc_row[-2:] == free_row[-2:]:
print('yes')
else:
print('No')
# 清除匹配的字符串malloc和字符串free
del malloc_row
del free_row
```