473,406 Members | 2,847 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

help on message splitting code

Expand|Select|Wrap|Line Numbers
  1. filename = message.split()[1]  
  2.     f = open(filename[1:])  
  3.         outputdata = f.read()
what is the actual function of code here. first and second lines are hard to figure out :-/
Feb 12 '12 #1
5 6707
bvdet
2,851 Expert Mod 2GB
To begin with, the indentation is wrong.

message is a string object. String method split() is used to break the string into parts using whitespace as a delimiter.
Expand|Select|Wrap|Line Numbers
  1. >>> s = "A simple\tstring\nobject."
  2. >>> s
  3. 'A simple\tstring\nobject.'
  4. >>> print s
  5. A simple    string
  6. object.
  7. >>> s.split()
  8. ['A', 'simple', 'string', 'object.']
  9. >>> 
The code expects element 1 ('simple' in the example above) to be the name of a file object with an unwanted prefix character.
Expand|Select|Wrap|Line Numbers
  1. >>> fn = s.split()[1]
  2. >>> fn[1:]
  3. 'imple'
  4. >>> 
A file object is created with built-in function open() and read with file object method read().

Really, this is very basic Python. All this information is readily available in the Python documentation. A good place to start is python.org.
Feb 12 '12 #2
i am familiar with split(),open() and read(). but my major concern is about the [1:].. i have neva come across such param with open(filename[1:]).

1.could u temme what [1:] signifies?
2.what is split function followed by [1] mean ?
Feb 12 '12 #3
bvdet
2,851 Expert Mod 2GB
@akilasekaran
1. Elements of sequence types are accessed by the index operator (s[i]), slice operator (s[i:j]) and extended slice operator (s[i:j:stride]). Given a string "abcdef":
Expand|Select|Wrap|Line Numbers
  1. >>> "abcdef"[1:]
  2. 'bcdef'
  3. >>> "abcdef"[:1]
  4. 'a'
  5. >>> "abcdef"[1]
  6. 'b'
  7. >>> "abcdef"[0:5:2]
  8. 'ace'
  9. >>> 
2. String method split() returns a list. Index operator s[1] returns the element at index 1. Note the first element in a list is at index 0.
Feb 13 '12 #4
dwblas
626 Expert 512MB
Joining and splitting strings http://www.diveintopython.net/native...ing_lists.html and splitting and slicing http://www.freenetpages.co.uk/hp/alan.gauld/tuttext.htm.
Feb 13 '12 #5
oh wow i totally understood it.. thanks dwblas and bvdet :)
Feb 13 '12 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: RickMuller | last post by:
I'm trying to split a string into pieces on whitespace, but I want to save the whitespace characters rather than discarding them. For example, I want to split the string '1 2' into . I was...
0
by: Hugh Haggerty | last post by:
I need help with VB6 code to direct a WAV file to the computer speaker. Anyone know how to do this?
2
by: Trint Smith | last post by:
Ok, My program has been formating .txt files for input into sql server and ran into a problem...the .txt is an export from an accounting package and is only supposed to contain comas (,) between...
4
by: Count László de Almásy | last post by:
Is there a standard way with optparse to include a blurb of text after the usage section, description, and the list of options? This is often useful to include examples or closing comments when...
7
by: alexander324 | last post by:
I have an access table that has around 4000 records showing various carrier names, origin and destination city, state and zip along distance and cost etc. Is there a way to automate a process that...
9
by: psantosh12 | last post by:
Hello everyone I need help to develop code for bug tracking system by using servlet and jsp technologies.. If anybody having code please send me the code. Regards Santosh
9
by: blazted | last post by:
I have a text file with a list of servers and then a description of the server along with the applications that may be hosted on them. Here is a example SERVER01 SERVER01 Windows Server 2003...
4
by: psycho007 | last post by:
Hi I have a caller ID software that I would like to change from Microsoft Access database to Mysql and was wondering if somebody could help me. The code i currently have is: Private Sub...
1
by: matan9234 | last post by:
hi, I need help in fitting code of buttons of website to all browsers (right now the code works only for explorer), can you tell me what i need to add to the code for the option of chrome/fire fox...
4
by: Bob Grer | last post by:
I need to write a short program in C to split a string value into two and assign them to two different variables. Example: Original string is "Switch1/Port1" split the string at the "/" and...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.