我有一个脚本,可以从输入文件中预测产品名称。代码如下:
output_dir = "C:\\Users\\Lenovo\\.spyder-py3\\NER_training"
DIR = 'C:\\Users\\Lenovo\\.spyder-py3\\Testing\\'
print("Loading from", output_dir)
nlp2 = spacy.load(output_dir)
with open('eng_productnames.csv', newline='') as myFile:
reader = csv.reader(myFile)
for rowz in reader:
try:
filenamez = rowz[1]
file = open(DIR+filenamez, "r", encoding ='utf-8')
filecontentszz = file.read()
for s in filecontentszz:
filecontentszz = re.sub(r'\s+', ' ', filecontentszz)
#filecontents = filecontents.encode().decode('unicode-escape')
filecontentszz = ''.join([line.lower() for line in filecontentszz])
doc2 = nlp2(filecontentszz)
for ent in doc2.ents:
print(filenamez, ent.label_, ent.text)
break
except Exception as e:`
以字符串的形式输出:
07-09-18 N021024s16PASBUNDLEACK - Acknowledgement P.txt PRODUCT ABC1
06-22-18 Letter from Supl.txt PRODUCT ABC2
06-22-18 Letter from Req to Change .txt PRODUCT ABC3
现在我想将所有这些细节导出到一个csv中,其中包含两列,一列作为FILENAME,另一列包含PRODUCT,在各自的列名下包含所有文件名和产品名。所有产品名称都以product开头,然后是字符串中的名称。如何解决这个问题:
输出csv应如下所示:
Filename PRODUCT
07-09-18 Acknowledgement P.txt ABC1
06-22-18 Letter Req to Change.txt ABC2
您可以使用
csv.writer
将每一行写入输出文件,使用writerow
而不是打印到屏幕上。你知道吗我在这里假设
filenamez
和ent.text
在每一列中都包含您想要的信息。如果不是这样的话,那么您可以在写入CSV之前操纵它们以获得所需的内容。你知道吗有很多方法可以做到这一点。我更喜欢使用Pandas,这是一个处理CSV文件的强大库。 您可以创建字典:
并将文件名和产品迭代地附加到相应的列表中。你知道吗
完成后,将预测的\u产品转换为数据帧,并调用\u csv函数:
我更喜欢这种方式,因为在保存文件之前可以更轻松地编辑数据。你知道吗
对于现有代码,我假设
print(filenamez, ent.label_, ent.text)
打印输出。如果是,那么:相关问题 更多 >
编程相关推荐