Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23247

Python output CSV data as formatted text file

$
0
0

I have an XLSX file which I am using python to convert to CSV to allow me to manipulate using pandas, I would then like to export the data from the CSV file into a formatted text document.

The xlsx data is as follows

NameNumberBranch Manager
Gustavo Barrett45Avery Page
Sterling Lynn199Matteo Hicks

I would like the format to be as follows for the TXT file

*****NEW RECORD*****Name : Gustavo BarrettNumber : 45Branch Manager : Avery Page*****RECORD END**********NEW RECORD*****Name : Sterling LynnNumber : 199Branch Manager : Matteo Hicks*****RECORD END*****

Below is my current code which prints all the names first, then the numbers and the branch managers, it prints them all separately, any idea how i can get it to output in the format i want?

import pandas as pdfrom pandas import DataFrameimport time with open("waffles.txt", "w") as file:    filename = 'finals.xlsx'    df = pd.DataFrame(pd.read_excel(filename))     names = df["Name"].to_list()    numbers = df["Number"].to_list()    branch = df["Branch Manager"].to_list()    for i in names:        for i in names:            txt_name = "Name : "+str(i)        for o in numbers:            txt_number = "Number : "+str(o)        for a in branch:            txt_branch = "Branch Manager : "+str(a)        file.write("\n")        file.write("*****NEW RECORD*****")        file.write("\n")        file.write(i+"\n")        file.write(str(o)+"\n")        file.write(a+"\n")        file.write("*****RECORD END*****")        file.write("\n")

Viewing all articles
Browse latest Browse all 23247

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>