Reputation: 33
I want to write simple python script which will first load .csv file and then output it to another file using provided template:
FirstVariable Cleartext-Password := "SecondVariable", Service-Type := Framed-User
Framed-IP-Address := ThirdVariable,
MS-Primary-DNS-Server := 8.8.8.8,
Fall-Through = Yes,
Mikrotik-Rate-Limit = 4thVariableM/5thVariableM
example:
input file:
john;lovelycat;192.168.1.1;40;30
output:
john Cleartext-Password := "lovelycat", Service-Type := Framed-User
Framed-IP-Address := 192.168.1.1,
MS-Primary-DNS-Server := 8.8.8.8,
Fall-Through = Yes,
Mikrotik-Rate-Limit = 40M/30M
Other parameters above should stay as they are, i only need to paste the mentioned values.
At this moment i know only how to read and write files, but i have no clue how to tell python to output text to file and how to use variables in the text so i can save the parameters from .csv to variables and then output it.
Could you please help me with tips how can i accomplish it?
Upvotes: 1
Views: 1067
Reputation: 82785
Using csv
module with f-string
Ex:
import csv
with open(filename) as infile:
reader = csv.reader(infile, delimiter=";")
for row in reader:
template_data = f"""{row[0]} Cleartext-Password := "{row[1]}", Service-Type := Framed-User
Framed-IP-Address := {row[2]},
MS-Primary-DNS-Server := 8.8.8.8,
Fall-Through = Yes,
Mikrotik-Rate-Limit = {row[3]}M/{row[4]}M"""
print(template_data)
Output:
john Cleartext-Password := "lovelycat", Service-Type := Framed-User
Framed-IP-Address := 192.168.1.1,
MS-Primary-DNS-Server := 8.8.8.8,
Fall-Through = Yes,
Mikrotik-Rate-Limit = 40M/30M
Upvotes: 1