使用 Python 从文本中获取 ASIN 并重新
Getting ASIN from text using Python and re
我正在尝试从我的文本中获取 asins。但我还没有实现。我尝试使用 re
但它对我帮助不大。它总是给我 None
结果。
这是我的文字:
/data.txt
FolkArt One Stroke Palette
FolkArt One Stroke Palette
B0007TZY3W
AMZRSG1890951279
4.6 review rate122
Deleter | Manga Tool Kit SPDX
Deleter | Manga Tool Kit SPDX
B000DZTROC
AMZRSG1890951289
4.6 review rate46
我想得到这个结果:
['B0007TZY3W', 'B000DZTROC']
这是我尝试过的:
# Get ASINS from data text file
with open('data.txt', 'r', encoding="utf8") as file:
data = file.read()
data = re.search(r'B(.*) AMZRSG', str(data))
print(data)
结果是:
None
我怎样才能达到这个结果?我试图用 re
得到它,但正如我所说,它没有用。希望你明白我的意思。谢谢。
您需要将换行符添加到您的正则表达式中:
import re
with open('data.txt', 'r', encoding="utf8") as file:
data = file.read()
asins = re.findall(r'B(.*)\nAMZRSG', str(data))
for asin in asins:
print(f'B{asin}')
输出:
B0007TZY3W
B000DZTROC
我正在尝试从我的文本中获取 asins。但我还没有实现。我尝试使用 re
但它对我帮助不大。它总是给我 None
结果。
这是我的文字:
/data.txt
FolkArt One Stroke Palette
FolkArt One Stroke Palette
B0007TZY3W
AMZRSG1890951279
4.6 review rate122
Deleter | Manga Tool Kit SPDX
Deleter | Manga Tool Kit SPDX
B000DZTROC
AMZRSG1890951289
4.6 review rate46
我想得到这个结果:
['B0007TZY3W', 'B000DZTROC']
这是我尝试过的:
# Get ASINS from data text file
with open('data.txt', 'r', encoding="utf8") as file:
data = file.read()
data = re.search(r'B(.*) AMZRSG', str(data))
print(data)
结果是:
None
我怎样才能达到这个结果?我试图用 re
得到它,但正如我所说,它没有用。希望你明白我的意思。谢谢。
您需要将换行符添加到您的正则表达式中:
import re
with open('data.txt', 'r', encoding="utf8") as file:
data = file.read()
asins = re.findall(r'B(.*)\nAMZRSG', str(data))
for asin in asins:
print(f'B{asin}')
输出:
B0007TZY3W
B000DZTROC