In [1]:
import csv

on a en entrée un fichier csv de cette forme :

cat routes_accra.csv |xsv sample 10 |xsv table
Lines  From             To                      network  mode  Operator                                                                Comment                                                   
281    Kaneshie         Russia                                 Accra Arena - Total Zongo - Russia - Mataheko Branch of G.P.R.T.U                                                                 
188    Circle           Asore Danho             Trotro   bus   Biakoye Taxi Union Branch of G.P.R.T.U                                                                                            
48     Abeka Lapaz      Korle Bu                Trotro   bus   Abeka-Lapaz Kaneshie Agbogbloshie Branch of GPRTU                                                                                 
164    Arena            Kaneshie                Trotro   bus   Accra Arena - Total Zongo - Russia - Marteheko Branch of G.P.R.T.U                                                                
256    Adjiringanor     Accra New Tema Station  Trotro   bus   East Legon-La Bawaleshie-M-A-S-A  GPRTU                                 Charge not recorded                                       
298    Kokomlemle       Dzorwulu                               Kokomlemle - Pig Farm - Dzorwulu - Abelemkpe Local of Pig Farm Branch.  Trotro loads en route                                     
96     Achimota         Kotobabi Down           Trotro   bus   Achimota GPRTU Main Branch                                              Not available                                             
389    Accra New Town   Madina                                 Accra Newtown - 37 - Burma Camp - Madina Branch                         B-track GPS has weak connection, A-track GPS is okay      
473    Accra Salaga     Chemuna                                Chorkor Trotro Branch of GPRTU                                                                                                    
177    Bubiashie-Atico  Circle                  Trotro   bus   Bubuashie-Atico-Circle Branch of GPRTU

In [2]:
with open("csv2wiki_routes_list.csv",'r') as f:
    reader = csv.DictReader(f)
    lines = list(reader)
    
#lines

In [3]:
wiki_template = """
|-
| rowspan="2" |%%ref%%
| rowspan="2" | %%network%%
| rowspan="2" | %%operator%%
| rowspan="2" |{{Relation|0|%%origin%%%%destination%%|tools=josm}}

{{User:Singing-Poppy/Template:InitPTRelations
|ref = %%ref%%
|from = %%origin%%
|to = %%destination%%
|operator = %%operator%%
|network = %%network%%
|pt_mode = %%mode%%
}}

||{{Relation|0|%%origin%%%%destination%%}}  || {{State Route|r=NA|h=NA}}
|| 
|
|-
|| {{Relation|0|%%destination%%%%origin%%}}  || {{State Route|r=NA|h=NA}}
|| 
| 
"""

for a_line in lines:
    #print(a_line)
    content = wiki_template.replace('%%ref%%', a_line['Lines'])
    content = content.replace('%%origin%%', a_line['From'])
    content = content.replace('%%destination%%', a_line['To'])
    content = content.replace('%%mode%%', a_line['mode'])
    content = content.replace('%%network%%', a_line['network'])
    content = content.replace('%%operator%%', a_line['Operator'])
    print(content)


|-
| rowspan="2" |2
| rowspan="2" | Trotro
| rowspan="2" | Osu Trotro Branch of GPRTU
| rowspan="2" |{{Relation|0|37 Lorry Park ↔ Osu (37 Station)|tools=josm}}

{{User:Singing-Poppy/Template:InitPTRelations
|ref = 2
|from = 37 Lorry Park
|to = Osu (37 Station)
|operator = Osu Trotro Branch of GPRTU
|network = Trotro
|pt_mode = bus
}}

||{{Relation|0|37 Lorry Park → Osu (37 Station)}}  || {{State Route|r=NA|h=NA}}
|| 
|
|-
|| {{Relation|0|Osu (37 Station) → 37 Lorry Park}}  || {{State Route|r=NA|h=NA}}
|| 
| 


|-
| rowspan="2" |3
| rowspan="2" | Trotro
| rowspan="2" | La YMCA Branch of GPRTU
| rowspan="2" |{{Relation|0|37 Lorry Park ↔ Tse Addo|tools=josm}}

{{User:Singing-Poppy/Template:InitPTRelations
|ref = 3
|from = 37 Lorry Park
|to = Tse Addo
|operator = La YMCA Branch of GPRTU
|network = Trotro
|pt_mode = bus
}}

||{{Relation|0|37 Lorry Park → Tse Addo}}  || {{State Route|r=NA|h=NA}}
|| 
|
|-
|| {{Relation|0|Tse Addo → 37 Lorry Park}}  || {{State Route|r=NA|h=NA}}
|| 
| 


|-
| rowspan="2" |4
| rowspan="2" | Trotro
| rowspan="2" | Abeka Lapaz Highway (GPRTU)
| rowspan="2" |{{Relation|0|Abeka Lapaz ↔ Ashaiman Main Station|tools=josm}}

{{User:Singing-Poppy/Template:InitPTRelations
|ref = 4
|from = Abeka Lapaz
|to = Ashaiman Main Station
|operator = Abeka Lapaz Highway (GPRTU)
|network = Trotro
|pt_mode = bus
}}

||{{Relation|0|Abeka Lapaz → Ashaiman Main Station}}  || {{State Route|r=NA|h=NA}}
|| 
|
|-
|| {{Relation|0|Ashaiman Main Station → Abeka Lapaz}}  || {{State Route|r=NA|h=NA}}
|| 
| 


|-
| rowspan="2" |5
| rowspan="2" | Trotro
| rowspan="2" | Abeka Lapaz Highway (GPRTU)
| rowspan="2" |{{Relation|0|Abeka Lapaz ↔ Ashalaja|tools=josm}}

{{User:Singing-Poppy/Template:InitPTRelations
|ref = 5
|from = Abeka Lapaz
|to = Ashalaja
|operator = Abeka Lapaz Highway (GPRTU)
|network = Trotro
|pt_mode = bus
}}

||{{Relation|0|Abeka Lapaz → Ashalaja}}  || {{State Route|r=NA|h=NA}}
|| 
|
|-
|| {{Relation|0|Ashalaja → Abeka Lapaz}}  || {{State Route|r=NA|h=NA}}
|| 
| 


|-
| rowspan="2" |6
| rowspan="2" | Trotro
| rowspan="2" | Abeka Lapaz Highway (GPRTU)
| rowspan="2" |{{Relation|0|Abeka Lapaz ↔ Sakumono Estate|tools=josm}}

{{User:Singing-Poppy/Template:InitPTRelations
|ref = 6
|from = Abeka Lapaz
|to = Sakumono Estate
|operator = Abeka Lapaz Highway (GPRTU)
|network = Trotro
|pt_mode = bus
}}

||{{Relation|0|Abeka Lapaz → Sakumono Estate}}  || {{State Route|r=NA|h=NA}}
|| 
|
|-
|| {{Relation|0|Sakumono Estate → Abeka Lapaz}}  || {{State Route|r=NA|h=NA}}
|| 
| 


|-
| rowspan="2" |7
| rowspan="2" | Trotro
| rowspan="2" | Abeka Lapaz Highway (GPRTU)
| rowspan="2" |{{Relation|0|Abeka Lapaz ↔ Taifa|tools=josm}}

{{User:Singing-Poppy/Template:InitPTRelations
|ref = 7
|from = Abeka Lapaz
|to = Taifa
|operator = Abeka Lapaz Highway (GPRTU)
|network = Trotro
|pt_mode = bus
}}

||{{Relation|0|Abeka Lapaz → Taifa}}  || {{State Route|r=NA|h=NA}}
|| 
|
|-
|| {{Relation|0|Taifa → Abeka Lapaz}}  || {{State Route|r=NA|h=NA}}
|| 
| 


|-
| rowspan="2" |8
| rowspan="2" | Trotro
| rowspan="2" | Abeka Lapaz Highway (GPRTU)
| rowspan="2" |{{Relation|0|Abeka Lapaz ↔ Tema Community One Market|tools=josm}}

{{User:Singing-Poppy/Template:InitPTRelations
|ref = 8
|from = Abeka Lapaz
|to = Tema Community One Market
|operator = Abeka Lapaz Highway (GPRTU)
|network = Trotro
|pt_mode = bus
}}

||{{Relation|0|Abeka Lapaz → Tema Community One Market}}  || {{State Route|r=NA|h=NA}}
|| 
|
|-
|| {{Relation|0|Tema Community One Market → Abeka Lapaz}}  || {{State Route|r=NA|h=NA}}
|| 
| 


|-
| rowspan="2" |9
| rowspan="2" | Trotro
| rowspan="2" | Lapaz Asofan Amasaman Branch of G.P.R.T.U.
| rowspan="2" |{{Relation|0|Abeka Lapaz ↔ Adeyman|tools=josm}}

{{User:Singing-Poppy/Template:InitPTRelations
|ref = 9
|from = Abeka Lapaz
|to = Adeyman
|operator = Lapaz Asofan Amasaman Branch of G.P.R.T.U.
|network = Trotro
|pt_mode = bus
}}

||{{Relation|0|Abeka Lapaz → Adeyman}}  || {{State Route|r=NA|h=NA}}
|| 
|
|-
|| {{Relation|0|Adeyman → Abeka Lapaz}}  || {{State Route|r=NA|h=NA}}
|| 
|