-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathWritepglistfile.py
33 lines (30 loc) · 1.1 KB
/
Writepglistfile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Description: write the contents in DownloadPgList.txt without same contents in increasing order
# Date: 4/22/2016
# Author: Yuanwei Wu
import os,re
# read the file
fold = open('DownloadPgList.txt', 'r')
content = fold.readlines()
lineset = set()
linelist = []
linetuple = ()
# set can't have same information
for line in content:
lineset.add(line)
# read each line, separate the number and url to a list
# then, put the separated two elements into tuple
# element 0 is str, need to change to number
for line2 in lineset:
mid = line2.split(' ')
urlCnt = int(mid[0]) # need to be float/int, the original is str
# urlName = str(mid[1]) # need to be str, the orginal is str, no need to change
linetuple = (urlCnt, mid[1])
linelist.append(linetuple)
# sort the linelist which is list of tuple
finalline = sorted(linelist, key=lambda x: x[0]) # set the parameters for sorted
# write the sorted url lists into file
for line3 in finalline:
# print line3
fnew = open('DownloadNewList.txt', 'a')
fnewline = fnew.write(str(line3)+'\n')# need to change line3 to str
# fnewline.close() # need to comment this, otherwise error