|
 |
|
 |
 |
|
05-04-2006, 07:46 AM
|
#16 (permalink)
|
|
Registered User
Join Date: Mar 2006
Posts: 25
|
ok i got the code working
Thanks i got it working finally but there one problem if the fields are to far apart in my text file then it dont insert the commas, but if the fields are close to togother den it insert the commas. but most of my text files will alway be far apart so i need some sort function dat need to include the all the white space and bring it close togother but i cant think of anything i been looking up the manual but i dont understand it
Code:
NUM,,,|FIRSTNAME|,SURNAME,|course,|
12400 COOL <to far away>WHATEVER CS
12500 MICHAEL <to far away> OWEN CS
12600 GEOGE FANNY CS
12700 CARMEN LIGHT IT
12800 PET ROOF
so i need to bring the fields close togother as well as inserting the commas,
__________________
|
|
|
05-04-2006, 07:47 AM
|
#17 (permalink)
|
|
Registered User
Join Date: Mar 2006
Posts: 25
|
got it working fully almost
Thanks i got it working finally but there one problem if the fields are to far apart in my text file then it dont insert the commas, but if the fields are close to togother den it insert the commas. but most of my text files will alway be far apart so i need some sort function dat need to include the all the white space and bring it close togother but i cant think of anything i been looking up the manual but i dont understand it
Code:
NUM,,,|FIRSTNAME|,SURNAME,|course,|
12400 COOL <to far away>WHATEVER CS
12500 MICHAEL <to far away> OWEN CS
12600 GEOGE FANNY CS
12700 CARMEN LIGHT IT
12800 PET ROOF
so i need to bring the fields close togother as well as inserting the commas,
__________________
|
|
|
05-04-2006, 08:15 AM
|
#18 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,681
|
You're not sure this has something todo with either <space> or <tab> seperating the items in your lines ?
The code will only transform spaces into commas, it wont do a thing if theres a <tab>, but this can be fixed by telling the compile() to search for ' |\t' ie:
Code:
for line in f.readlines():
returnline.append(re.compile('( |\t)').sub(',', line[0:-1]))
|
|
|
05-04-2006, 10:11 AM
|
#19 (permalink)
|
|
Registered User
Join Date: Mar 2006
Posts: 25
|
its just shows blank
when i ran the code it just shows blank
__________________
|
|
|
05-04-2006, 10:20 AM
|
#20 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,681
|
hmm.... thats odd:
Code:
#!/usr/bin/python
import re
returnline = []
f = open("List", "r")
for line in f.readlines():
returnline.append(re.compile('( |\t)').sub(',', line[0:-1]))
for item in returnline:
print item
Quote:
> ./test.py
NUM,|FIRSTNAME|,SURNAME,|course,|
------------------------------------------
12400,ERIC,RONNEY,CS
------------------------------------------
12500,MICHAEL,OWEN,CS
------------------------------------------
12600,SCOT,MAN,CS
------------------------------------------
12700,GEOGE,YO,IT
------------------------------------------
|
|
|
|
05-04-2006, 10:37 AM
|
#21 (permalink)
|
|
Registered User
Join Date: Mar 2006
Posts: 25
|
sorry its works
thanks its work fully
__________________
|
|
|
05-11-2006, 05:55 AM
|
#22 (permalink)
|
|
Registered User
Join Date: Mar 2006
Posts: 25
|
I found an easier way to understand how dis work
but it dont work fully
Code:
def formate(datafile):
returnlines = ""
currentline = datafile.readline()
while currentline:
if currentline[0] != ('#'):
returnlines+= currentline +'\n'
currentline = datafile.readline()
return returnlines
def listdata(filename):
datafile = open(filename, 'r')
returnlines = []
for currentline in datafile.readlines():
if not currentline.startswith('#'):
returnlines.append(currentline.replace('\n', '').split(','))
currentline = datafile.readline()
datafile.close()
return returnlines
def presentdata(showdata):
for item in showdata:
print item
print '\n'
to exercute the code
>>>datafile = open("StudentList.txt", "r")
>>>eventdata = datafile.readlines()
>>>print eventdata
__________________
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 11:58 AM.
|
Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle
|
 |
|