Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
.sort frustration
#1
Hi,

I've tried so many different ways to sort but nothing is working so I've just left it as fh.sort (I know that's wrong) and I'm hoping someone can figure it out.

My code is:

1 import sys
2
3 f=open(sys.argv[1]).read().split("\n")
4 fh=open(sys.argv[2], 'wr+')
5
6 length = len(f)
7
8 for n,line in enumerate(f):
9 if "----- Begin chunk -----" in line:
10 if n == length:
11 break
12 nlist = n + 1
13 print >> fh, (nlist)
14 for m,line in enumerate(f):
15 if "----- End chunk -----" in line:
16 if m == length:
17 break
18 mlist = m + 1
19 print >> fh, (mlist)
20 fh.sort

I want to output the fh file as a numerically sorted version that looks like this:

1 1
2 5
3 50
4 75
5 100
6 150

Please and thank you!
Reply
#2
That's the full code I've posted already.
The error I get with this code is below, but I've gotten loads of different errors trying different ways of sorting.

Traceback (most recent call last):
File "loop.py", line 21, in <module>
fh.sort()
AttributeError: 'file' object has no attribute 'sort'
Reply
#3
Please Read BBCODE to see how to use code tags.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Photo a.sort() == b.sort() all the time 3lnyn0 1 1,311 Apr-19-2022, 06:50 PM
Last Post: Gribouillis
  Geany frustration how to deal with tabs and spaces and CR mlytle0 2 4,442 Sep-14-2017, 11:51 AM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020