python怎么修改txt中的单词_python - 如何通过Python将.txt文件中的单词与.xlsx文件中的单词进行比较? 然后,我将其写入新的.xls文件中以提取这些单词 - 堆栈内存溢...

我想根据多个.xlsx文件中的单词比较多个.txt文件中的常用单词。 这些是文本文件的外观:

文字档1:

(

apples), kkkk, alloal

df, ja_tee

ffgg

hchh.hc

yuytu_member

文字档2:

(

orange pplL

df

ffgg

hchhhc

yuytu_

文字档3:

(

pear +lpdlpla;s

df

ffgg

hchhhc

yuytu_

我试图提取apples , orange和pear 。 这些值在我的.xlsx文件的单元格A1中找到。

即.xlsx文件1:单元格A1 =苹果

.xlsx文件2:单元格A1 =橙色

.xlsx文件3:单元格A1 =梨

到目前为止,这是我的代码:

import os, sys

import openpyxl

from openpyxl.reader.excel import load_workbook

import xlwt

from xlwt import Workbook

#The filepath that I will be saving my .xls file to:

filepath = ('C:/Users/xxxx/Documents/xxx/abc.xls')

#The .xls file:

wb2 = xlrd.open_workbook('C:\\Users\\xxxx\\Documents\\xxxx\\abc.xls', on_demand= True)

wb2 = Workbook()

sheet2 = wb2.add_sheet("xyz", cell_overwrite_ok=True)

#The .xlxs file that contains the words i want to compare w .txt file:

folder_path1 = os.chdir("C:/Users/xxx/Documents/xxxx/Test python dict")

for file in os.listdir(folder_path1):

if file.endswith(".xlsx"):

wb = load_workbook(file, data_only=True)

ws = wb.active

cell_range = ws['A1']

dictionary = cell_range.value

print(dictionary) #to check if the words/values are there

# Me writing the name of each .txt file to the .xls file:

for r, dir in enumerate(os.listdir("C:/Users/xxx/Documents/xxxx/txt test python")):

sheet2.write(r+1,1,dir)

#Reading .txt file and trying to make the sentence into words instead of lines so that I can compare the .txt individual words with the .xlsx file:

import glob

path = "C:/Users/xxx/Documents/xxxx/txt test python"

from os.path import isfile

files=filter(isfile,glob.glob('%s/*'%path))

for name in files:

with open(name) as texts:

data1 = texts.read().strip()

import re

data = re.split('[,.\n\s]',data1)

for current_word in data:

txtwords = current_word

if txtwords in dictionary:

print(txtwords)

#sheet2.write(r+1,2,dir)

# wb2.save(filepath)

但是,我无法实现我想要的结果。 我现在的输出是:

apples

orange

pear

pear

为什么只打印一个常用字?

预期产量:

apple

orange

pear

apples

orange

pear

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值