473,327 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Sorting Countries by Region

Hi all,

I'm analyzing some data that has a lot of country data. What I need
to do is sort through this data and output it into an excel doc with
summary information. The countries, though, need to be sorted by
region, but the way I thought I could do it isn't quite working out.
So far I can only successfully get the data alphabetically.

Any ideas?

import xlrd
import pyExcelerator

def get_countries_list(list):
countries_list=[]
for country in countries:
if country not in countries_list:
countries_list.append(country)

EU = ["Austria","Belgium", "Cyprus","Czech Republic",
"Denmark","Estonia", "Finland"]
NA = ["Canada", "United States"]
AP = ["Australia", "China", "Hong Kong", "India", "Indonesia",
"Japan"]
Regions_tot = {'European Union':EU, 'North America':NA, 'Asia
Pacific':AP,}

path_file = "c:\\1\country_data.xls"
book = xlrd.open_workbook(path_file)
Counts = book.sheet_by_index(1)
countries= Counts.col_values(0,start_rowx=1, end_rowx=None)

get_countries_list(countries)

wb=pyExcelerator.Workbook()
matrix = wb.add_sheet("matrix")

n=1
for country in unique_countries:
matrix.write(n,1, country)
n = n+1

wb.save('c:\\1\\matrix.xls')

Nov 16 '07 #1
6 2161
Just a few notes:

1) get_countries_list

What is the purpose of that function? Besides a few errors (an
argument named list, no value returned), it seems you just want to
remove duplicates from a list called countries. You can do that
transforming the list to a 'set'.

new_countries = list( set(countries) )

2) I would suggest using countries.sort(...) or sorted(countries,...),
specifying cmp or key options too sort by region instead.

3) Instead of doing this:

for country in unique_countries:
matrix.write(n,1, country)
n = n+1

Do something like

for i, cou in enumerate(unique_countries):
matrix.write(1+i, 1, cou)

(so you dont need to increment the 'i' variable manually)
On Nov 16, 2007 2:13 PM, <pa***********@gmail.comwrote:
Hi all,

I'm analyzing some data that has a lot of country data. What I need
to do is sort through this data and output it into an excel doc with
summary information. The countries, though, need to be sorted by
region, but the way I thought I could do it isn't quite working out.
So far I can only successfully get the data alphabetically.

Any ideas?

import xlrd
import pyExcelerator

def get_countries_list(list):
countries_list=[]
for country in countries:
if country not in countries_list:
countries_list.append(country)

EU = ["Austria","Belgium", "Cyprus","Czech Republic",
"Denmark","Estonia", "Finland"]
NA = ["Canada", "United States"]
AP = ["Australia", "China", "Hong Kong", "India", "Indonesia",
"Japan"]
Regions_tot = {'European Union':EU, 'North America':NA, 'Asia
Pacific':AP,}

path_file = "c:\\1\country_data.xls"
book = xlrd.open_workbook(path_file)
Counts = book.sheet_by_index(1)
countries= Counts.col_values(0,start_rowx=1, end_rowx=None)

get_countries_list(countries)

wb=pyExcelerator.Workbook()
matrix = wb.add_sheet("matrix")

n=1
for country in unique_countries:
matrix.write(n,1, country)
n = n+1

wb.save('c:\\1\\matrix.xls')

--
http://mail.python.org/mailman/listinfo/python-list
Nov 16 '07 #2
pa***********@gmail.com wrote:
Hi all,

I'm analyzing some data that has a lot of country data. What I need
to do is sort through this data and output it into an excel doc with
summary information. The countries, though, need to be sorted by
region, but the way I thought I could do it isn't quite working out.
So far I can only successfully get the data alphabetically.

Any ideas?

import xlrd
import pyExcelerator

def get_countries_list(list):
it isn't a good idea to use a built-in object as a variable name
countries_list=[]
for country in countries:ii
if country not in countries_list:
countries_list.append(country)

EU = ["Austria","Belgium", "Cyprus","Czech Republic",
"Denmark","Estonia", "Finland"]
NA = ["Canada", "United States"]
AP = ["Australia", "China", "Hong Kong", "India", "Indonesia",
"Japan"]
Regions_tot = {'European Union':EU, 'North America':NA, 'Asia
Pacific':AP,}
i would create a class to capture country information, e.g.
class country(object):
def __init__(self, name, size = 0, population = 0):
self.name = name
self.size = size
self.poplation = population

def __cmp__(self, other):
if self.name < other.name:
return -1
elif self.name other.name:
return 1
else:
return 0

then you can set up the world as

world = {'NA': [country("United States"), country("Canada")], \
'Europe': [country("Belgium"), country("Austria")]}
now you can sort and print it easy

for region in world:
print region
lands = world[region]
lands.sort()
for land in lands:
print land.name

the sort works because the country objects have a method __cmp__
>
path_file = "c:\\1\country_data.xls"
book = xlrd.open_workbook(path_file)
Counts = book.sheet_by_index(1)
countries= Counts.col_values(0,start_rowx=1, end_rowx=None)

get_countries_list(countries)

wb=pyExcelerator.Workbook()
matrix = wb.add_sheet("matrix")

n=1
for country in unique_countries:
matrix.write(n,1, country)
n = n+1

wb.save('c:\\1\\matrix.xls')
i'm not familiar with the excel modules so i can't help you with that
Nov 16 '07 #3
On Nov 16, 8:28 pm, martyw <wit...@kpnplanet.nlwrote:
>
i would create a class to capture country information, e.g.
<snip>

What is the advantage of this:
def __cmp__(self, other):
if self.name < other.name:
return -1
elif self.name other.name:
return 1
else:
return 0
over
def __cmp__(self,other):
return cmp(self.name,other.name)

?

--
Alan
Nov 16 '07 #4
En Sat, 17 Nov 2007 08:34:43 -0300, <pa***********@gmail.comescribió:
for i, country in countries_list:
if country in REGIONS_COUNTRIES['European Union']:
matrix.write(i+2, 1, country)
but I got "ValueError: too many values to unpack"
Remove the i, and try again...

--
Gabriel Genellina

Nov 17 '07 #5

On Sat, 2007-11-17 at 03:34 -0800, pa***********@gmail.com wrote:
#This seems to not work today and I don't know why
#for country in countries_list:
# if country not in REGIONS_COUNTRIES['European Union'] or not in
REGIONS_COUNTRIES['North America']:
# print "%s is not in the expected list", country
This snippet fails because you want 'and' rather than 'or'. In this
one, you test to see that country is not in EU, and if you have, say
'France' as your country, the first half evaluates false, so the or
tells it to try again with North America. Lo and behold, france is not
in North America, so the second half returns true. False or True
returns True, so your code prints "France is not in the expected list."

Cheers,
Cliff
Nov 17 '07 #6
nothing, Alan wrote:
On Nov 16, 8:28 pm, martyw <wit...@kpnplanet.nlwrote:
>i would create a class to capture country information, e.g.
<snip>

What is the advantage of this:
> def __cmp__(self, other):
if self.name < other.name:
return -1
elif self.name other.name:
return 1
else:
return 0

over
def __cmp__(self,other):
return cmp(self.name,other.name)

?

--
Alan
yours is better!
Nov 19 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ddaniel | last post by:
I have read many posts and seen many papers on the different techniques for sort and filtering datagrids. Many do re-queries against the dB ala Fritz Onion. I am trying to leverage the Dataview....
19
by: Owen T. Soroke | last post by:
Using VB.NET I have a ListView with several columns. Two columns contain integer values, while the remaining contain string values. I am confused as to how I would provide functionality to...
6
by: Arthur Dent | last post by:
How do you sort a generic collection derived from System.Collections.ObjectModel.Collection? Thanks in advance, - Arthur Dent
0
by: Maran | last post by:
We have come across a situation that I thinks not many have. Grateful for all responses. Regards Maran ************* * Scenario A DataList binds to a DataRow, with "RegionName" och...
5
by: Cindy Lee | last post by:
I'm getting my data from an XML file. The data binds fine, but I can't sort on it. Do I have to do anything special? I have enabled sorting on my grid view. Autogenerate columns is off, and I...
4
by: =?Utf-8?B?SGFucyAtIERpYUdyYXBoSVQgLQ==?= | last post by:
Hi! It's me again with my silly questions. Does .Net framework 1.1 hold any list of all the countries in the world/region? I'm deveolping a windowsform and I need to list all available...
22
by: Ubi | last post by:
I am looking for a list of countries and nationalities, against the top level domain. es: ..ar ==Argentina ==Argentinian ..al ==Albania ==Albanian etc. etc.
0
by: rupalirane07 | last post by:
Both grids displays fine. But the problem is only parent datagrid sorting works fine but when i clik on child datagrid for sorting it gives me error: NullReferenceException error Any...
1
by: dorandoran | last post by:
The sort on the childgrid is not working; nothing happens when I click on the each column header for sort. (I followed Satay's sample: http://www.codeproject.com/KB/aspnet/EditNestedGridView.aspx)...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.