On Wed, Mar 3, 2010 at 8:19 AM, John Filben wrote:
> I am new to Python but have used many other (mostly dead) languages in the
> past. I want to be able to process *.txt and *.csv files. I can now read
> that and then change them as needed – mostly just take a column and do some
> if-then to cr
MRAB wrote:
[snip]
Simpler would be:
lines = f.readlines()
lines.sort(key=lambda line: line[ : 3])
or even:
lines = sorted(f.readlines(), key=lambda line: line[ : 3]))
Sure, but a complete newbie (I have this impression about OP) doesn't
have to know about lambda.
I expected
John, there's an error in my program, I forgot that list.sort() method
doesn't return the list (it sorts in place). So it should look like:
#!/usr/bin/python
def sortit(fname):
fo = open(fname)
linedict = {}
for line in fo:
key = line[:3]
linedict[key] = line
sor
MRAB writes:
> mk wrote:
>> John Filben wrote:
>>> I am new to Python but have used many other (mostly dead) languages
>>> in the past. I want to be able to process *.txt and *.csv files.
>>> I can now read that and then change them as needed – mostly just
>>> take a column and do some if-then t
mk wrote:
John Filben wrote:
I am new to Python but have used many other (mostly dead) languages in
the past. I want to be able to process *.txt and *.csv files. I can
now read that and then change them as needed – mostly just take a
column and do some if-then to create a new variable. My p
John Filben wrote:
I am new to Python but have used many other (mostly dead) languages in
the past. I want to be able to process *.txt and *.csv files. I can
now read that and then change them as needed – mostly just take a column
and do some if-then to create a new variable. My problem is s
I am new to Python but have used many other (mostly dead) languages in the
past. I want to be able to process *.txt and *.csv files. I can now read that
and then change them as needed – mostly just take a column and do some if-then
to create a new variable. My problem is sorting these files: