easy questions from python newbie

John Machin sjmachin at lexicon.net
Sun Jul 23 23:34:55 EDT 2006


James Stroud wrote:
> walterbyrd wrote:
> > This is the first real python program I have ever worked on. What I
> > want to do is:
> > 1) count identical records in a cvs file
> > 2) create a new file with quantities instead duplicate records
> > 3) open the new file in ms-excel
> >
> > For example, I will start with a file like:
> >
> > 1001
> > 1012
> > 1008
> > 1012
> > 1001
> > 1001
> >
> > and finish with a file like:
> >
> > 1001,3
> > 1008,1
> > 1012,2
> >
> > What I need to know:
> > 1) is there a function in python that will sort a file into another
> > file. Something like:
> > sort file1.txt > file2.txt from the DOS command line. I know there is
> > also a similar "sort" funtion in Unix.
>
> import os
> os.system('sort file1.txt > file2.txt')
>
> > 2) is there a function in python that will correctly load a csv file
> > into an excel spreadsheet,
> > and open that spreadsheet.
>
> What's with the excel files? You must be in industry. Excel opens csv
> files, no problem. In Mac OS X, you can do this:
>
>    os.system('open -a /Applications/Excel %s' % 'my_file.csv')
>
> It probably requires a nauseating journey through a bunch of hoops to do
> the equivalent in window$. But you haven't specified your operating
> system, so I optimistically assume a best-case (OS X) and not a
> worst-case (window$). If worst-case, I'm truly sorry for your misfortune.

Hey d00d, what's with the attit00d? All those $ signs? Do you get Excel
for free on Mac OS X or does your department pay for it?  Us 'doze
dummies w/o a big endowment like UCLA wouldn't/couldn't afford that.
Out here in the boonies we have to download the free stuff.

C:\junk>python
Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit (Intel)]
on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.system('"C:\Program Files\OpenOffice.org 2.0\program\scalc" foo.csv')
0
>>>

Look, d00d, no hoops.

>
> > 3) I will probably be working with 50 items, or less, would it be best
> > for me to do this with a
> > multi-diminsional array? For example: sort the file, read a rec into
> > the array, if the next rec is the same then incr the count, otherwise
> > add a new rec with a count of 1. Then write the array to a file?
> >
>
> Ah, a real question. Use a dict:
>
> if adict.has_key(some_key):

Hey, d00d, ask the department sysadmin to update your Python for you,
then you'll be able to use this:

    if some_key in adict:

>    adict[some_key] += 1
> else:
>    adict[some_key] = 1
>




More information about the Python-list mailing list