4
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?

Mohammad Yusuf
  • 16,554
  • 10
  • 50
  • 78
Erandi
  • 719
  • 3
  • 7
  • 16
  • 3
    http://stackoverflow.com/questions/2081640/what-exactly-do-u-and-r-string-flags-do-in-python-and-what-are-raw-string-l – jkr Dec 04 '16 at 05:20
  • 3
    The docs can. https://docs.python.org/2/library/stdtypes.html#str.split – Klaus D. Dec 04 '16 at 05:22

2 Answers2

12

Raw string vs Python string

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'

  1. Split on all commas and break into n+1 parts where n is the number of commas:
>>>print 'hello,world,there,you,are'.split(',')
['hello', 'world', 'there', 'you', 'are']
  1. Split on first comma and break only into 2 parts.
>>>'hello,world,there,you,are'.split(',',1)  
['hello', 'world,there,you,are']
  1. Split on first and second comma and break into 3 parts. And so on...
>>>'hello,world,there,you,are'.split(',',2)
['hello', 'world', 'there,you,are']

And even more...

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)
[]

And even...



.

.

.

What?

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)

jarmod
  • 71,565
  • 16
  • 115
  • 122
Mohammad Yusuf
  • 16,554
  • 10
  • 50
  • 78
1

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.

tupui
  • 5,738
  • 3
  • 31
  • 52