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 03-29-2006, 01:35 PM   #1 (permalink)
coolman
Registered User
 
Join Date: Mar 2006
Posts: 25
coolman is on a distinguished road
split funtion in python

does any one know how to use the split function in pthyon I want to know how to split up numbers and names from a input file

for example

122323fred
334344man
coolman is offline   Reply With Quote
Old 03-30-2006, 06:14 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
redhead is on a distinguished road
If you have the lines in a file ie:
Code:
f = open("path/to/filename")
   lines = f.readlines()
   for line in lines:
       # do something with line
       line_head = line[start:6]
       line_tail = line[6:-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 04-04-2006, 05:00 AM   #3 (permalink)
coolman
Registered User
 
Join Date: Mar 2006
Posts: 25
coolman is on a distinguished road
i dont understant the file name bit

i have the file in a directory called my documents and another directory called computer science and the file is there

Code:
f = open("mydocuments/computerscience/student.txt")
   lines = f.readlines()
   for line in lines:
       # do something with line
       line_head = line[start:6]
       line_tail = line[6:-1]
but this dont work any ideas
coolman is offline   Reply With Quote
Old 04-04-2006, 07:12 AM   #4 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
redhead is on a distinguished road
Works for me:
Code:
~> cat test.pl
#!/usr/bin/python
#
f = open("stud.txt")
lines = f.readlines()
for line in lines:
     # do something with line
     line_head = line[0:6]
     line_tail = line[6:-1]
     print "Line Head: " + line_head
     print "Line Tail: " + line_tail
~> cat stud.txt
122323fred
334344man
~> ./test.pl
Line Head: 122323
Line Tail: fred
Line Head: 334344
Line Tail: man
__________________
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 04-25-2006, 12:47 PM   #5 (permalink)
coolman
Registered User
 
Join Date: Mar 2006
Posts: 25
coolman is on a distinguished road
Hi this is not working

the file is in a directory called Python, but this still dont work I am trying to read in the file

Code:
f = open('/Python/Stud.txt) 
lines = f.readlines()   #lines in the file
for line in lines:      #loop line is variable
     print line      #to test
Please I have been stuck for ages
coolman is offline   Reply With Quote
Old 04-25-2006, 01:35 PM   #6 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
Remove the leading slash from "/Python/Stud.txt" . I suspect you meant "Python/Stud.txt" since I doubt /Python is a folder off root.
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Old 04-25-2006, 02:20 PM   #7 (permalink)
coolman
Registered User
 
Join Date: Mar 2006
Posts: 25
coolman is on a distinguished road
I got a different error message

Code:
f = open('Python/Stud.txt) 
lines = f.readlines()   #lines in the file
for line in lines:      #loop line is variable
     print line      #to test
	 
SyntaxError: EOL while scanning single-quoted string]
coolman is offline   Reply With Quote
Old 04-25-2006, 04:05 PM   #8 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
you left off a single quote this time "f = open('Python/Stud.txt) " should be "f = open('Python/Stud.txt')"
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Old 04-26-2006, 02:20 PM   #9 (permalink)
coolman
Registered User
 
Join Date: Mar 2006
Posts: 25
coolman is on a distinguished road
sorry it still dont work

Python24 is a directory and student is a text file and i am trying to read in that file but its not working. so the pathway is just wrong any ideas

Code:
f = open('Python24/Student')
lines = f.readlines()
for line in lines:
      line_head = line[0:6]
     line_tail = line[6:-1]
     print "Line Head: " + line_head
     print "Line Tail: " + line_tail
Traceback (most recent call last):
File "<pyshell#0>", line 1, in -toplevel-
f = open('Python24/Student')
IOError: [Errno 2] No such file or directory: 'Python24/Student'
coolman is offline   Reply With Quote
Old 04-26-2006, 03:28 PM   #10 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
is it case sensitive? Are you running t from a folder that has Pyhton24/Student below it?
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Old 04-27-2006, 04:48 AM   #11 (permalink)
coolman
Registered User
 
Join Date: Mar 2006
Posts: 25
coolman is on a distinguished road
Thanks man

Its work But i had to use 'r' for reading the file but i got it to work thanks man
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
web log in using python prashmohan All Other Coding Languages 2 12-16-2005 12:46 AM
Python problem... slashdot All Other Coding Languages 0 02-01-2005 09:54 PM
New Tutorial: Python Lists Overview sde Code Newbie News 3 03-24-2004 09:54 AM
PyCon 2004 - Python Developers Conference sde Code Newbie News 0 02-12-2004 03:45 PM


All times are GMT -8. The time now is 01:17 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





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