def loadTest(filename):
f=open(filename,'r')
k=0
line=f.readline()
labels=[]
vectors=[]
while line and k<4:
k=k+1
l=line[:-1].split(r'","')
s=float(l[0][1:])
tweet=l[5][:-1]
print(l)
line=f.readline()
f.close()
return
What do split(r'","') actually does inside python split method?
r'","'The r is to indicate it's a raw string.
How is a raw string different to a regular python string?
The special characters lose their special meaning inside a raw string. For example \n is a newline character inside a python string which will lose its meaning in a raw string and will simply mean backslash followed by n.
string.split()string.split() will break and split the string on the argument that is passed and return all the parts in a list. The list will not include the splitting character(s).
string.split('","') will break and split the string on every "," and return all the broken parts in a list excluding ","
Eg:
print 'Hello world","there you are'.split(r'","')
Output:
['Hello world', 'there you are']
split() can do even more...You can specify how many parts you want your string to break into by passing in an extra parameter.
Lets consider this string: 'hello,world,there,you,are'
>>>print 'hello,world,there,you,are'.split(',')
['hello', 'world', 'there', 'you', 'are']
>>>'hello,world,there,you,are'.split(',',1)
['hello', 'world,there,you,are']
>>>'hello,world,there,you,are'.split(',',2)
['hello', 'world', 'there,you,are']
From the docs:
If splitting character(s) i.e separator is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace. Consequently, splitting an empty string or a string consisting of just whitespace with a None separator returns [].
For example,
>>>' 1 2 3 '.split()
['1', '2', '3']
>>>' 1 2 3 '.split(None, 1)
['1', '2 3 ']
>>>''.split()
[]
>>>' '.split()
[]
>>>' '.split(None)
[]
.
.
.
Isn't it enough that you are looking for more? Don't be so greedy :P.
Just question yourself?, it will make you non-greedy :D (You will get the joke if you know regular expressions)
It splits the string into items of a list. For example
'I am a string'.split(' ')
Gives the output
['I', 'am', 'a', 'string']
Explanation:
Here, I used ' ' as a split so every time a ' ' character comes, it splits the string into a new item of list.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With