13/06/2019, 09:53
13/06/2019, 11:13
13/06/2019, 22:25
14/06/2019, 11:49
# mypath should be the complete path for the directory containing the input text files
mypath = 'C:/Users/Michela/Desktop/eserc_python/PROVA_IMPORT/prova1/NetCart'
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]
#import xlwt
import xlrd
from xlwt import Workbook
from os.path import exists
import time
import os
#import xlsxwriter
# controllo l'esistenza dei file. Se esistono li elimino
if(exists('generated_xls.xls' and 'log1.txt')):
os.remove('generated_xls.xls')
os.remove('log1.txt')
time.sleep(2)
# apro il file .txt e la riorganizzo in un file .xlsx
for textfile in textfiles:
f = open(textfile, 'r+', encoding='utf-8')
row_list = []
for string in f:
s = string.split()
t = s[0]
q = s[1]
r = s[2]
str = [t,q,r]
column_list = zip(str)
wb = Workbook()
sheet = wb.add_sheet('Sheet1')
sheet2 = wb.add_sheet('Sheet2')
i=0
for column in column_list:
for item in range(len(column)): #purtroppo viene sovrascitta la 1a riga
sheet.write(item, i, column[item])
wb.save(join(mypath, 'generated_xls.xls'))
i+=1
f.close()
# Per fare la prova continuo importando manualmente i dati.
# Apro il file .xlsx, leggo le righe controllando che l'argomento non sia lo stesso.
# Questo mi consente di contare il numero dei nodi, il numero di link per ogni nodo.
# Vorrei poter ciclare anche sulla seconda colonna per verificare il numero di
# ciascuna interazione.
wb1 = xlrd.open_workbook('network.xlsx')
sheet3 = wb1.sheet_by_index(0)
y = sheet3.ncols
x = sheet3.nrows
fx = open('log1.txt','w+', encoding='utf-8')
for column in range(0,y):
countLINK = 1
count_LINK = 1
for rows in range(0,x):
A = sheet3.cell_value(rows,column)
B = sheet3.cell_value(rows + 1,column)
C = sheet3.cell_value(rows - 1,column)
if (A == B or A == C):
# #print("UGUALE = " + str(countLINK) + " " + A)
countLINK += 1
if A != B :
# vorrei provare a scrivere in una seconda scheda dello stesso file .xlsx
#sheet2.write(rows, i, column[rows])
# print(A, file = fx)
print(A + " = " + str(countLINK - 1), file = fx)
# fx.write(A + " = " + str(countLINK - 1))
# fx.write(A + " = " + str(countLINK - 1))
countLINK = 0
else:
# print('preso', file = fx)
# fx.write(A + " = " + str(count_LINK) + '\n')
# fx.write(A + " = " + str(count_LINK))
print(A + " = " + str(count_LINK), file = fx)
fx.close()
14/06/2019, 20:07
Moderatore: apatriarca
i=0
for column in column_list:
for item in range(len(column)):
sheet.write(item, i, column[item])
i
rimane uguale a zero per tutti i cicli. Suppongo che quello che volessi fare era qualcosa come for i, column in enumerate(column_list):
for item in range(len(column)):
sheet.write(item, i, column[item])
if(exists('generated_xls.xls' and 'log1.txt')):
'generated_xls.xls' and 'log1.txt'
è uguale a 'log1.txt'
e stai quindi semplicemente verificando l'esistenza di questo ultimo file. Non mi è inoltre chiaro lo scopo di inserire uno sleep in questo caso.
14/06/2019, 22:16
14/06/2019, 23:50
wb = Workbook()
sheet = wb.add_sheet('Sheet1')
row = 0
for textfile in textfiles:
with open(textfile, 'r+', encoding='utf-8') as f:
for line in f:
items = line.split()
for col, label in enumerate(items):
sheet.write(row, col, label=label)
row += 1
wb.save(join(mypath, 'generated_xls.xls'))
15/06/2019, 01:43
16/06/2019, 00:01
16/06/2019, 01:19
Skuola.net News è una testata giornalistica iscritta al Registro degli Operatori della Comunicazione.
Registrazione: n° 20792 del 23/12/2010.
©2000—
Skuola Network s.r.l. Tutti i diritti riservati. — P.I. 10404470014.
Powered by phpBB © phpBB Group - Privacy policy - Cookie privacy
phpBB Mobile / SEO by Artodia.