Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 05-04-2006, 08:46 AM   #16 (permalink)
coolman
Registered User
 
Join Date: Mar 2006
Posts: 25
coolman is on a distinguished road
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,
coolman is offline   Reply With Quote
Old 05-04-2006, 08:47 AM   #17 (permalink)
coolman
Registered User
 
Join Date: Mar 2006
Posts: 25
coolman is on a distinguished road
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,
coolman is offline   Reply With Quote
Old 05-04-2006, 09:15 AM   #18 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
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]))
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 05-04-2006, 11:11 AM   #19 (permalink)
coolman
Registered User
 
Join Date: Mar 2006
Posts: 25
coolman is on a distinguished road
its just shows blank

when i ran the code it just shows blank
coolman is offline   Reply With Quote
Old 05-04-2006, 11:20 AM   #20 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
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
------------------------------------------
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 05-04-2006, 11:37 AM   #21 (permalink)
coolman
Registered User
 
Join Date: Mar 2006
Posts: 25
coolman is on a distinguished road
sorry its works

thanks its work fully
coolman is offline   Reply With Quote
Old 05-11-2006, 06:55 AM   #22 (permalink)
coolman
Registered User
 
Join Date: Mar 2006
Posts: 25
coolman is on a distinguished road
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
coolman is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
file types: reading and writing pre_wreck Standard C, C++ 2 09-26-2005 06:49 AM
Reading in a file jello Standard C, C++ 4 05-04-2004 08:53 AM
Binary I/O file reading (0x1a trouble) Danish Standard C, C++ 2 05-26-2003 11:02 AM


All times are GMT -8. The time now is 03:55 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0 RC8 ©2007, Crawlability, Inc.





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting