检测数组中以X开头的所有单词的代码:Python
有人向我解释我不明白的地方吗/ 错误检测数组中以X开头的所有单词的代码:Python,python,arrays,sorting,syntax,Python,Arrays,Sorting,Syntax,有人向我解释我不明白的地方吗/ 错误 import urllib.request import csv import http.client import bs4 import Data import re from bs4 import BeautifulSoup from Data import DA #Extracting Html code from the URL with urllib.request.urlopen("http://www.apecbraking.co.uk/
import urllib.request
import csv
import http.client
import bs4
import Data
import re
from bs4 import BeautifulSoup
from Data import DA
#Extracting Html code from the URL
with urllib.request.urlopen("http://www.apecbraking.co.uk/catalogue/frictionParts/range/783854") as url:
D = url.read()
#HTML Code *
soup = BeautifulSoup(D)
Table = (soup.table)
soup = (Table.get_text())
#Creates 'APEC.csv'
file = open("APEC.csv", "w")
file.write(soup)
file.close()
print('Written')
# Set the Data into a list so it can be refered back to later on e.g searching for only words with certain letter
# from there they can be seperated from one list into categories (Variables)
newRow = []
with open('APEC.csv') as f:
reader = csv.reader(f)
for row in reader:
print()
#New List
newRow.append(row)
print(re.findall('A'))
回溯(最近一次呼叫最后一次):
文件“C:/Users/B8/Desktop/Task/TaskOriginal.py”,第49行,在
打印(newRow.findall('A'))
AttributeError:“list”对象没有属性“findall”
“忽略”必须添加更多细节以进行更新
SDFDSFFFFFFFFFFFFFFFFFFFFFDFSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
您的
newRow
具有类型列表
。没有与此类型关联的方法。您可以通过在python控制台中键入help(list)
来获取此类型的所有可用方法的列表。尝试使用
Traceback (most recent call last):
File "C:/Users/B8/Desktop/Task/TaskOriginal.py", line 49, in <module>
print(newRow.findall('A'))
AttributeError: 'list' object has no attribute 'findall'
您需要导入正则表达式库
with open('yourfile') as f:
new_list = [row for row in f if row.startswith('A')]
或者试试这段代码
import re
不是正则表达式(
re
)库的一部分吗?您正在调用list.findAll()
。我现在将尝试导入re库,但仍然无济于事。这不是.findall()
,而是re.findall()
。请修改你的帖子的格式,因为它看起来很糟糕。你的帖子可以工作,但它只能找到一个单词,而不能找到所有的单词。这很奇怪。。请尝试打印这些行,看看它们是否按预期分割<它仍然打印['A1[2010-]\n'],其中有5行以上以字母a开头>它使用此括号打印?如果是这样,请尝试if row.startswith('[A'),如果我添加了'['
print([item for item in newRow if item[0]=='A'])