0
当前程序我在下面检查目录中的多个文件以查找特定单词,然后将值存储在5个列表中每个文件的人名,地址,角色等。将多个列表写入电子表格中的多列,并设置列标题
我现在正在努力做的事情是,将列表分隔成各自的值,然后将每个列表中的值垂直保存在自己的列中,从而有效地为电子表格中的每个文件保存一行已扫描的目录。我还需要有成为每个列的列标题上零行
from os import walk
import os
import os, re, string
from os import walk
from xlutils.copy import copy
from xlrd import open_workbook
import xlwt
folderpath = "./filepath" #Path for the files
book = open_workbook("spreadsheetname.xlsx") #Name of the spreadsheet
OUTPUT_NAME = ("spreadsheetname.xlsx")
peopleList = [] #List for People Column
asnList = [] #List for ASN Column
organizationList = [] #List for Organization Column
roleList = [] #List for Role Column
addressList = [] #List for Address Column
worksheet = copy(book); NAME =(); SHEET_1 = worksheet.get_sheet(0)
bookS = xlwt.Workbook()
row = 0
column = 0
for(path, dirs, files) in os.walk(folderpath, topdown=True):
for filename in files:
print(filename)
filepath = os.path.join("./filepath", filename)
with open(filepath, 'r') as currentfile:
for line in currentfile:
people = re.findall(r"person: (.*)",line)
asn = re.findall(r"aut-num: (.*)",line)
organization = re.findall(r"organisation: (.*)",line)
role = re.findall(r"role: (.*)",line)
address = re.findall(r"address: (.*)",line)
people = map(lambda x: x.strip(), people)
asn = map(lambda x: x.strip(), asn)
organization = map(lambda x: x.strip(), organization)
role = map(lambda x: x.strip(), role)
address = map(lambda x: x.strip(), address)
for person in people:
peopleList.append(person)
for asn_c in asn:
asnList.append(asn_c)
for organ_c in organization:
organizationList.append(organ_c)
for role_c in role:
roleList.append(role_c)
for address_c in address:
addressList.append(address_c)
for person in set(sorted(peopleList)):
SHEET_1.write(row, column, person)
row += 1
for asn_c in set(sorted(asnList)):
SHEET_1.write(row, column, asn_c)
row += 1
for organ_c in set(sorted(organizationList)):
SHEET_1.write(row, column, organ_c)
row += 1
for role_c in set(sorted(roleList)):
SHEET_1.write(row, column, role_c)
row += 1
for address_c in set(sorted(addressList)):
SHEET_1.write(row, column, address_c)
row += 1
bookS.save(OUTPUT_NAME)
我没用过xlwt - 也许你有强迫你使用它的约束 - 但作为另一种选择我之前使用过python的csv库,发现很容易做类似的事情。 https://docs.python.org/2/library/csv.html。 – knoight