0

Having a hard time understanding what is happening in this snippet of code. Particularly with the 2nd line of code.

for line in infile:
 data = line.strip('\n').split(':')
 user_dict[data[0]] = data[1]
Eli Sadoff
7,2986 gold badges37 silver badges64 bronze badges
asked Dec 5, 2016 at 7:13
2
  • You mean the data = line? Which part of the line you don't understand? Commented Dec 5, 2016 at 7:16
  • probably you mean the third line, because second line is quite straightforward. if you have trouble to understand second line, u could get help from docs.python.org/2/library/string.html, strip() means removing the leading and trailing character, in your case it means remove leading and trailing '\n'. For third line, data[0] and data[1] are both string, so it meas a dictionary named as user_dict, and its index is data[0] and its value is data[1]. Commented Dec 5, 2016 at 7:20

7 Answers 7

1

The line sets the variable data equal to the string represented by the variable line with the new line character '\n' removed and then split anywhere a : occurs.

answered Dec 5, 2016 at 7:18

Comments

1

It parses a file having this structure:

a:52
b:hi
key:value

for line in infile: is a loop for each line in the file. Each line (except for the last maybe) ends with new-line symbol \n.

line.strip('\n') removes the new-line symbol.

.split(':') splits the string into strings there were separated by :. For example: "qwe:rty:uio".split(':') -> ["qwe", "rty", "uio"]

user_dict[data[0]] = data[1] obviously saves the data into the dicionary user_dict taking the first string as a key, and second one as a value.

For the file mentioned above this code creates the following dictionary:

{"a": "52", "b": "hi", "key": "value"}
answered Dec 5, 2016 at 7:51

Comments

0

line.strip('\n') is removing all the \n (new line) from the string and the split(':') it is going to split your string using : as the delimeter into an array of strings.

answered Dec 5, 2016 at 7:18

Comments

0

Above code is storing the file into the dictionary. Content of the file is like below

key1:value1
key2:value2
.
.
.
key3:value3

Second line is stripping off the \n character from the line and then splitting the each line by : character. However you should try to understand and debug the code line by line

answered Dec 5, 2016 at 7:19

Comments

0

line.strip('\n') will remove all the newlines from the string.

and

split(':') will split your string using ':' into array of strings.

answered Dec 5, 2016 at 7:20

1 Comment

line.strip doesn't do that ... see my comment above
0
data = line.strip('\n').split(':')

There are two string functions in one line. You also can separate the calls. This should be the same:

my_line = line.strip('\n')
my_line1 = my_line.split(':')

line.strip --> removes the new line character at the end of a line line.split(':') --> splits the values at colon character and return a list of each record

It is easier to understand with concrete values.

Your file look like this and you loop through each line. Name: Paul Age: 18 Gender: Male

At the end of each line you have a "new line" character which will remove line.strip('\n'). Then you split the values at ":" You finally create a dictionary (line 3) where key is the left side and the value is the right side. dict['Name'] = 'Paul' dict['Age'] = '18'

help-info.de
7,36817 gold badges43 silver badges43 bronze badges
answered Dec 5, 2016 at 7:29

1 Comment

line.strip(newline) removes leading consecutive newlines and trailing consecutive newlines, but leaves embedded newlines alone. >>> "NNNtttNNNtttNNN".strip("N") produces 'tttNNNttt'
0

Basically line.strip('\n') removes leading consecutive newlines and trailing consecutive newlines, but leaves embedded newlines alone. from line; and then split(':') separates anywhere ":" is.This is then stored as a list in the variable named data.

answered Dec 5, 2016 at 7:23

1 Comment

line.strip doesn't do that ... see my comment on previous answer

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.