[Tutor] csv manipulation
bob gailer
bgailer at gmail.com
Wed Oct 29 16:28:46 CET 2008
qsqgeekyogdty at tiscali.co.uk wrote:
> hello,
> i have the follwoing csv file:
>
> "Berat","Berat","Kuçovë","Skrapar"
> "Dibër","Bulqizë","Dibër","Mat"
> "Durrës","Durrës","Krujë"
> "Elbasan","Elbasan","Gramsh","Librazhd","Peqin"
> "Fier","Fier","Lushnjë","Mallakastër"
> "Gjirokastër","Gjirokastër","Përmet","Tepelenë"
> "Korçë","Devoll","Kolonjë","Korçë","Pogradec"
> "Kukës","Has","Kukës","Tropojë"
> "Lezhë","Kurbin","Lezhë","Mirditë"
> "Shkodër","Malësi e Madhe","Pukë","Shkodër"
> "Tirana","Kavajë","Tirana"
> "Vlorë","Delvinë","Sarandë","Vlorë"
>
> where thee first column contains the regions and the subsequent the
> districts.
>
> what is the best way to return a file like:
>
> "Berat","Kuçovë"
> "Berat","Skrapar"
> "Dibër","Bulqizë"
> "Dibër","Dibër"
> "Dibër","Mat"
>
> etc...
>
> and to write it out as a new file.
>
>
There is a csv module, but for something this simple the following will
suffice:
input = open(path-to-the-input-file, 'r')
output = open(path-to-the-output-file, 'w')
for line in input:
line = line.split(",") # ["Berat", "Berat", "Kuçovë", "Skrapar"]
region = line [0]
for district in line[1:]:
output.write(region + "," + district + "\n")
input.close()
output.close()
--
Bob Gailer
Chapel Hill NC
919-636-4239
When we take the time to be aware of our feelings and
needs we have more satisfying interatctions with others.
Nonviolent Communication provides tools for this awareness.
As a coach and trainer I can assist you in learning this process.
What is YOUR biggest relationship challenge?
More information about the Tutor
mailing list