Reputation: 595
I have 9 sub directories which have three files and I want to write those files to an Excel file. I start by reading all sub directories and then I convert the file to list then to dataframe which I export to an Excel file using "to_excel" and "writer excel" but for a strange reasons, the code does not produces any file.
# Path to the different files
path = r"C:\Users\Emmanuelle\Documents\tal\retrotraduction\corpus_amazon\corpus_retraduit"
for root, subdirs, files in os.walk(path):
#print(root)
for file in files:
print(file)
f_name = file[:-7]
print(f_name)
#print(files)
print("-----File in processed :", file)
with open(os.path.join(root, file), "r", encoding='utf-8') as b_translate_file:
liste = [line.rstrip() for line in b_translate_file]
if liste[0] != 'Contenu':
#print(liste)
if len(liste) == 2020:
print("-------------")
print("-----File of freins category identified :" , len(liste))
print("-------------")
df = pd.DataFrame(liste)
print(df)
writer = pd.ExcelWriter(os.path.join(path, "/{}.xlsx".format(f_name)), engine ='xlsxwriter')
df.to_excel(writer, sheet_name = f_name)
I expected file with 2020 elements to be write to the excel file.
df looks like this
----File of freins category identified : 2020
-------------
0
0 Malheureusement, l'impression de violence, bie...
1 Tout cela ne me donne pas envie d'utiliser un ...
2 """Mettre 5 étoiles dans le pétrin pour cet al...
3 "c'est bien écrit, c'est fluide, la seule pris...
4 Oui, bien sûr, il y a la super introduction de...
... ...
2015 m'a plongé dans une nuit blanche pour ce roman...
2016 Ce disque n'est pas mauvais en soi, mais il ne...
2017 "En voulant changer l'esprit de la série, les ...
2018 "Voici le déclin et la décadence d'une ancienn...
2019 "C'est l'ensemble le plus complet, à ma connai...
Upvotes: 2
Views: 262
Reputation: 114
Try simplifying this:
os.path.join(path, "/{}.xlsx".format(f_name))
into "{}.xlsx".format(f_name)
Also, how about trying:
df.to_excel(path_name, sheet_name=f_name)
instead, without the use of ExcelWriter?
Upvotes: 2