【发布时间】:2020-11-12 19:07:45
【问题描述】:
我正在从网络上抓取数据,并尝试使用 BRAND、MODEL、PN...等来构建这些数据。
阅读How to extract brand from product name 并发现无法通过计算机完成所有操作,因此我决定同时使用人和计算机。
原始数据示例:['NOKIA 5.3 phone (black)', 'NOKIA 5.3 phone (white)', 'NOKIA 5.3 phone']
def textbetween(text):
s = text
start = '('
end = ')'
s_int = s.find(start,3)
s_end = s.find(end,3)+1
found = s[s_int:s_end]
return found
def textcheck(TEXT):
spec_temp = textbetween(TEXT)
remove_spec = TEXT.replace(spec_temp, '')
check = remove_spec.split(' ')
return check
在上面之后我会得到一个单词列表并使用 for 循环从原始数据中读取它[1, 2, 3...]
我试图创建一些空列表并使用条件来
raw = ['NOKIA 5.3 phone (black)', 'NOKIA 5.3 phone (white)', 'NOKIA 5.3 phone ']
BRAND = []
MODEL = []
PN = []
OTHER = []
for i in raw:
check = textcheck(i)
for temp in check:
while temp not in BRAND or MODEL or PN:
print(temp, '<=Brand? leave blank if no, or type anything as Yes')
human= input()
if human!= '':
print('this is a brand, add to brand list')
BRAND.append(temp)
break
elif human == '':
print(temp, '<=Model? leave blank if no, or type anything as Yes')
human= input()
if human!= '':
print('this is a model, add to model list')
MODEL.append(temp)
break
elif human== '':
print(temp, '<=PN? leave blank if no, or type anything as Yes')
human= input()
if human!= '':
print('this is a PN, add to PN list')
PN.append(temp)
break
else:
print('Do nothing')
break
else:
pass
else:
pass
在上述条件下,我应该得到一个 BRAND、MODEL、PN 的列表 - 人类第一次定义,机器检查单词是否在 BRAND 列表中,任何原始数据都可以流式传输到这个结构。 到目前为止,定义空列表不起作用 - 在我运行第一个产品名称之后,品牌已经有了诺基亚,但第二次机器再次询问我哪个诺基亚已经在列表中。因为列表是基于输入的原始数据和人工定义的动态列表,我如何运行它并使条件适用于对数据进行排序?
并计划导出列表excel之类的
- 原始数据:NOKIA 5.3 手机(黑色) 品牌:NOKIA 型号:5.3 PN:--
- 品牌列表(存储以备后用)
- 型号列表(存储以备后用)
RAW 数据更新 https://drive.google.com/drive/folders/1SHLKqOLjL5wJpkEoC_C_peD3Ogwt8tVS?usp=sharing
【问题讨论】:
-
你期望输出什么??
-
品牌、型号、PN、其他 => 表现出色。主要是为了确保原始数据可以按列表的条件进行排序。
-
@wiskinglin 发布“原始数据示例”的预期输出
-
@更木健八。在excel中更新为这样的图片。谢谢
-
@wiskinglin 发布一些包含 PN 和不同品牌的示例数据
标签: python list conditional-statements