easy questions from python newbie

W

walterbyrd

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.
2) is there a function in python that will correctly load a csv file
into an excel spreadsheet,
and open that spreadsheet.
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?
 
J

James Stroud

walterbyrd said:
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.
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):
adict[some_key] += 1
else:
adict[some_key] = 1

James



--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
 
J

John Machin

James said:
import os
os.system('sort file1.txt > file2.txt')


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.
Look, d00d, no hoops.
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
 
J

James Stroud

John said:
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.


I use FC4 and open office. I had to fire up a window$ machine to test
the CSV thing, d00d.
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.

0


Look, d00d, no hoops.

Glad for you.
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

Last I checked, both worked.

James


--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
 
M

Marc 'BlackJack' Rintsch

walterbyrd said:
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.

Lists have a sort method. No need to do this with temporary files. Just
read in the first file into a list and sort it.
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?

I would read the file into a list of list, that's what comes closest to a
multidimensional array, via the `csv` module. Sort that (outer) list and
then use `itertools.groupby()` to group the identical lists. You can
write the rows with the `csv` module again. Short example:

import csv
from itertools import groupby

in_file = open('test.csv', 'rb')
data = list(csv.reader(in_file))
in_file.close()

data.sort()

out_file = open('test2.csv', 'wb')
writer = csv.writer(out_file)
for row, identical_rows in groupby(data):
row.append(len(list(identical_rows)))
writer.writerow(row)
out_file.close()

Ciao,
Marc 'BlackJack' Rintsch
 
W

walterbyrd

Thanks to all who replied.

As I mentioned, I am new to python. I will have to look some of this
stuff, but that is fine. I am trying to learn.

I am sorry I forgot to mention, the platform is windows-xp. I am doing
this for a client who has a small warehouse operation. Personally, I
usually use debian at home.

If it matters, the origianl file is coming from a portable scanner.
After it's uploaded to the PC, the file needs to be hand processed,
then loaded into an remote site with a mysql backend.

When new items are recieved, they are scanned into a portable device,
then transferred to a file on a PC. The python app will then process
that file to provide item counts so that the shipment can be easily
compared to the packing list (there has been a lot of trouble with
shipments not matching). Once the list is checked, the items will have
to placed into bins - this is why I need the file to be loaded into
excel. The corresponding bin numbers will have to entered into that
spreadsheet, then saved to a cvs, then the cvs will be processed into
the online database.
 
T

Tim

James said:
snip...
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

Last I checked, both worked.

James

Alternatively, the whole if can be replaced with:-

adict[some_key] = adict.get(some_key, 0) + 1

Tim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top