У меня есть текстовый файл n чисел строк и столбцов, разделенных тремя разделителями [
, ]
, -
.
Мне нужно импортировать это в Excel, используя Python.
Примеры данных моего текстового файла:
AM[38070] 22220-22-0-0-0-0-1-126-0-0-1-0-0-255-0
AM[38070] 22-0-0-1-1-126-0-0-0-126-0-4095-2047-2047-1
Обратите внимание на r
, n
количество строк и столбцов, мне нужно записать все в файл Excel.
Я пробовал приведенный ниже код, но он учитывает только один разделитель и работает только для 2 столбцов:
# mypath should be the complete path for the directory containing the input text files
mypath = raw_input("Please enter the directory path for the input files: ")
from os import listdir
from os.path import isfile, join
textfiles = [ join(mypath,f) for f in listdir(mypath) if isfile(join(mypath,f)) and '.txt' in f]
def is_number(s):
try:
float(s)
return True
except ValueError:
return False
import xlwt
import xlrd
style = xlwt.XFStyle()
style.num_format_str = '#,###0.00'
for textfile in textfiles:
f = open(textfile, 'r+')
row_list = []
for row in f:
row_list.append(row.split('['))
column_list = zip(*row_list)
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('Sheet1')
i = 0
for column in column_list:
for item in range(len(column)):
value = column[item].strip()
if is_number(value):
worksheet.write(item, i, float(value), style=style)
else:
worksheet.write(item, i, value)
i+=1
workbook.save(textfile.replace('.txt', '.xls'))