下面是将源目录作为方法参数的方法,它返回一个数组。 此方法正在执行的操作是转到word文档的源目录,提取所有word文档的信息,然后将它们存储在数组中。然后,该方法返回此数组,其中包含单词文档列表。
问题是如何使用返回的数组并在另一个python文件中调用该方法?请帮忙,谢谢! :((
def get_ednotes(str2):
directory = str2
my_text = docx2txt.process(directory)
#path = 'C:\\Users\\L31307\\PycharmProjects\\FYP\\keys1.txt'
path = "..\keys1.txt"
ED_Notes_file = open(path, 'r')
key_list = ED_Notes_file.read().split(':')
my_text = my_text.replace('\n', '')
my_text = my_text.replace('\xa0', '')
new = my_text.split('Created By')
new.pop(0)
word = "Created By"
new2 = [word + x for x in new]
def find_between(s, first, last):
try:
start = s.index(first) + len(first)
end = s.index(last, start)
if(end !=2):
return s[start:end]
else:
return'NA'
except ValueError:
return "NA"
# print(len(new2))
split = []
key_list = ['Created By :', 'Created On :', 'Stage :', 'Notes :'] * len(new2)
for i in new2:
a = find_between(i, key_list[0], key_list[1])
a = a.replace(':','')
split.append(a)
b = find_between(i, key_list[1], key_list[2])
b = b.replace(':','')
split.append(b)
c = find_between(i, key_list[2], key_list[3])
c = c.replace(':', '')
split.append(c)
d = i.split("Notes:", 1)[1]
split.append(d)
x = 0
final_objects = []
n = int(len(split)/4)
for i in range(0, n):
result = make_student(split[x], split[x+1], split[x+2], split[x+3])
x = x+4
final_objects.append(result)
getFilenameOnly = os.path.basename(directory)
#docNumber = ('_'.join(getFilenameOnly.split('_')[:1]))
categoryType = ('_'.join(getFilenameOnly.split('_')[1:3]))
result = {}
count = 0
dic = {}
array = []
arr_cat = []
arr_new = []
for i in final_objects:
count = count + 1
s = str(count)
value = []
value2 = []
#dic['Created By'] = i.Created_By
#dic['Created On'] = i.Created_On
#dic['Stage'] = i.Stage
dic['Notes'] = i.Notes
s = dict(dic)
array.append(s)
final_list = {'Category': categoryType, 'Notes': array}
arr_new += [final_list]
dict2str = str(final_list)
c = cleanString(newstring=dict2str)
arr_cat += [c]
new_arr = []
for i in range(0, len(arr_cat)):
r = '|'.join(str(arr_cat[i]).split(" ", 1))
new_arr += [r]
# open file to append the items in the array to the previously written textfile
with open('ED_Notes.txt', 'a') as append_txtfile:
for item in new_arr:
append_txtfile.write("%s\n" % item)
return new_arr
root_dir = "../dataprep/source_documents"
for filename in os.listdir(root_dir):
str2 = root_dir + '/' + filename
get_ednotes(str2)
答案 0 :(得分:1)
您可以这样做
file1.py:
def get_ednotes(str2):
directory = str2
my_text = docx2txt.process(directory)
...
file2.py:
from file1 import get_ednotes
root_dir = "../dataprep/source_documents"
for filename in os.listdir(root_dir):
str2 = root_dir + '/' + filename
arr = get_ednotes(str2)
print arr