Python: retrieving two separate list values in one file line
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I am working on a code in python where there is two separate values in one specific line of a file. I want to retrieve both of them as separate parts to a list for matplotlib. This is the code that I have so far:
with open('data.txt') as data_file:
def process(line):
line = line.rstrip(data_file)
line = line.split('.')[1]
line = line.split(',')
return line
x = list()
y = list()
counter = 0
for line in data_file:
if (counter == 3) or (counter == 4):
result = process(line)
x.append(int(result[0]))
y.append(int(result[1]))
counter += 1
print(x)
print(y)
The error is saying:
line = line.rstrip(data_file)
TypeError: rstrip arg must be None or str
A sample file is:
hi
hi
67, 78
2345, 45677
Can someone please help me fix this error, or provide a better way to achieve the same outcome. Any help is appreciated!
Thanks so much!
python python-3.x macos matplotlib
|
show 2 more comments
I am working on a code in python where there is two separate values in one specific line of a file. I want to retrieve both of them as separate parts to a list for matplotlib. This is the code that I have so far:
with open('data.txt') as data_file:
def process(line):
line = line.rstrip(data_file)
line = line.split('.')[1]
line = line.split(',')
return line
x = list()
y = list()
counter = 0
for line in data_file:
if (counter == 3) or (counter == 4):
result = process(line)
x.append(int(result[0]))
y.append(int(result[1]))
counter += 1
print(x)
print(y)
The error is saying:
line = line.rstrip(data_file)
TypeError: rstrip arg must be None or str
A sample file is:
hi
hi
67, 78
2345, 45677
Can someone please help me fix this error, or provide a better way to achieve the same outcome. Any help is appreciated!
Thanks so much!
python python-3.x macos matplotlib
1
Why do you have the argumentdata_file
in the lineline = line.rstrip(data_file)
?
– Xteven
Jan 3 at 4:15
@Xteven I thought thatline = line.rstrip()
needed to reference the file to strip the line
– EMCMAHE
Jan 3 at 4:17
1
rstrip
is a method belonging to any string object in Python which accepts an optional argument of a string (which contains which characters to strip). More information here: docs.python.org/3/library/stdtypes.html#str.rstrip
– Xteven
Jan 3 at 4:21
@Xteven thanks so much for your help! I have one more question if that's ok. When I removed thedata_file
from my code it said that theline = line.split('.')[1]
list index is out of range. Do u know how I can solve that?
– EMCMAHE
Jan 3 at 4:26
In your sample file, none of the lines have the character.
in them, which means thatline.split(".")
just returns a list of whatever was in that line because there is nothing to split by. The return list only has one item (whatever was in the line), so Python is complaining about trying to access the second item.
– Xteven
Jan 3 at 4:29
|
show 2 more comments
I am working on a code in python where there is two separate values in one specific line of a file. I want to retrieve both of them as separate parts to a list for matplotlib. This is the code that I have so far:
with open('data.txt') as data_file:
def process(line):
line = line.rstrip(data_file)
line = line.split('.')[1]
line = line.split(',')
return line
x = list()
y = list()
counter = 0
for line in data_file:
if (counter == 3) or (counter == 4):
result = process(line)
x.append(int(result[0]))
y.append(int(result[1]))
counter += 1
print(x)
print(y)
The error is saying:
line = line.rstrip(data_file)
TypeError: rstrip arg must be None or str
A sample file is:
hi
hi
67, 78
2345, 45677
Can someone please help me fix this error, or provide a better way to achieve the same outcome. Any help is appreciated!
Thanks so much!
python python-3.x macos matplotlib
I am working on a code in python where there is two separate values in one specific line of a file. I want to retrieve both of them as separate parts to a list for matplotlib. This is the code that I have so far:
with open('data.txt') as data_file:
def process(line):
line = line.rstrip(data_file)
line = line.split('.')[1]
line = line.split(',')
return line
x = list()
y = list()
counter = 0
for line in data_file:
if (counter == 3) or (counter == 4):
result = process(line)
x.append(int(result[0]))
y.append(int(result[1]))
counter += 1
print(x)
print(y)
The error is saying:
line = line.rstrip(data_file)
TypeError: rstrip arg must be None or str
A sample file is:
hi
hi
67, 78
2345, 45677
Can someone please help me fix this error, or provide a better way to achieve the same outcome. Any help is appreciated!
Thanks so much!
python python-3.x macos matplotlib
python python-3.x macos matplotlib
edited Jan 6 at 19:03
EMCMAHE
asked Jan 3 at 4:07
EMCMAHEEMCMAHE
327
327
1
Why do you have the argumentdata_file
in the lineline = line.rstrip(data_file)
?
– Xteven
Jan 3 at 4:15
@Xteven I thought thatline = line.rstrip()
needed to reference the file to strip the line
– EMCMAHE
Jan 3 at 4:17
1
rstrip
is a method belonging to any string object in Python which accepts an optional argument of a string (which contains which characters to strip). More information here: docs.python.org/3/library/stdtypes.html#str.rstrip
– Xteven
Jan 3 at 4:21
@Xteven thanks so much for your help! I have one more question if that's ok. When I removed thedata_file
from my code it said that theline = line.split('.')[1]
list index is out of range. Do u know how I can solve that?
– EMCMAHE
Jan 3 at 4:26
In your sample file, none of the lines have the character.
in them, which means thatline.split(".")
just returns a list of whatever was in that line because there is nothing to split by. The return list only has one item (whatever was in the line), so Python is complaining about trying to access the second item.
– Xteven
Jan 3 at 4:29
|
show 2 more comments
1
Why do you have the argumentdata_file
in the lineline = line.rstrip(data_file)
?
– Xteven
Jan 3 at 4:15
@Xteven I thought thatline = line.rstrip()
needed to reference the file to strip the line
– EMCMAHE
Jan 3 at 4:17
1
rstrip
is a method belonging to any string object in Python which accepts an optional argument of a string (which contains which characters to strip). More information here: docs.python.org/3/library/stdtypes.html#str.rstrip
– Xteven
Jan 3 at 4:21
@Xteven thanks so much for your help! I have one more question if that's ok. When I removed thedata_file
from my code it said that theline = line.split('.')[1]
list index is out of range. Do u know how I can solve that?
– EMCMAHE
Jan 3 at 4:26
In your sample file, none of the lines have the character.
in them, which means thatline.split(".")
just returns a list of whatever was in that line because there is nothing to split by. The return list only has one item (whatever was in the line), so Python is complaining about trying to access the second item.
– Xteven
Jan 3 at 4:29
1
1
Why do you have the argument
data_file
in the line line = line.rstrip(data_file)
?– Xteven
Jan 3 at 4:15
Why do you have the argument
data_file
in the line line = line.rstrip(data_file)
?– Xteven
Jan 3 at 4:15
@Xteven I thought that
line = line.rstrip()
needed to reference the file to strip the line– EMCMAHE
Jan 3 at 4:17
@Xteven I thought that
line = line.rstrip()
needed to reference the file to strip the line– EMCMAHE
Jan 3 at 4:17
1
1
rstrip
is a method belonging to any string object in Python which accepts an optional argument of a string (which contains which characters to strip). More information here: docs.python.org/3/library/stdtypes.html#str.rstrip– Xteven
Jan 3 at 4:21
rstrip
is a method belonging to any string object in Python which accepts an optional argument of a string (which contains which characters to strip). More information here: docs.python.org/3/library/stdtypes.html#str.rstrip– Xteven
Jan 3 at 4:21
@Xteven thanks so much for your help! I have one more question if that's ok. When I removed the
data_file
from my code it said that the line = line.split('.')[1]
list index is out of range. Do u know how I can solve that?– EMCMAHE
Jan 3 at 4:26
@Xteven thanks so much for your help! I have one more question if that's ok. When I removed the
data_file
from my code it said that the line = line.split('.')[1]
list index is out of range. Do u know how I can solve that?– EMCMAHE
Jan 3 at 4:26
In your sample file, none of the lines have the character
.
in them, which means that line.split(".")
just returns a list of whatever was in that line because there is nothing to split by. The return list only has one item (whatever was in the line), so Python is complaining about trying to access the second item.– Xteven
Jan 3 at 4:29
In your sample file, none of the lines have the character
.
in them, which means that line.split(".")
just returns a list of whatever was in that line because there is nothing to split by. The return list only has one item (whatever was in the line), so Python is complaining about trying to access the second item.– Xteven
Jan 3 at 4:29
|
show 2 more comments
1 Answer
1
active
oldest
votes
this is what i could come up with:
import re
regex = r'[d]{1,3}, [d]{1,3}'
result =
with open('sample.txt') as f:
lines = f.readlines()
for line in lines:
match = re.findall(regex, line)
if match != :
splitted = match[0].split(',')
#the values are mapped to a list containing floating point numbers
mapped = list(map(float, splitted))
#and then are appended to a list that will contain all of
#the lines that have the numbers on it
result.append(mapped)
print(result)
#this is how you could access each line in result
for list in result:
print(list)
output
[[67.0, 78.0], [25.0, 18.0]] #result is a list containing all lines that have the pattern <number>, <number>
[67.0, 78.0] #the first line that matches the pattern
[25.0, 18.0] #the second one
this uses regular expressions to look for numbers up to 3 digits (but you can change that to whatever you want), matching the pattern <number>, <number>
if it matches the pattern, it splits the two numbers at the ,
creating a list containing those two values and appends them to the result list
Hope it helps.
Any questions feel free to ask.
Edit
im using this as a sample file to exemplify to you:
hi
hi
67, 78
hi again
25, 18
Thanks so much for your help! I am a very beginner at python so I don't really understand what's going on - even though you explained some of it! Do you mind explaining to me what re is, findall is, regex is, and finally the match != part. I know that this is basically the whole entire code, but I would really appreciate your help. Thanks again!
– EMCMAHE
Jan 3 at 14:36
1
“ re” is a built-in package for python that handles regular expressions. When “re” is imported, it works kind of a class, “findall” being one of the methods it has. “findall”, in this case takes two arguments, the regular expression “regex”, and a string (being whatever is on the line of the sample file)
– Gustavo Barros
Jan 3 at 14:54
1
"match" is the value that "re.findall" returns. If it matches the regex, its a list with all the matches in a line (in this case there is only one in one line). The "!=" is only an operator, it simply translates to "not equal to", and in this case, an empty list, meaning it will only append the matches that contains the regular expression.
– Gustavo Barros
Jan 3 at 15:03
1
I highlly suggest checking out the python documentation: docs.python.org/3, expecially the beginner's guide: docs.python.org/3.7/tutorial/index.html, it really helped me out some time ago. If you can't find what you're looking for in those pages, try searching in the index modules: docs.python.org/3.7/py-modindex.html
– Gustavo Barros
Jan 3 at 15:19
1
I made an edit for mapping the values from strings to floats using the map function
– Gustavo Barros
Jan 3 at 15:40
|
show 6 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54016234%2fpython-retrieving-two-separate-list-values-in-one-file-line%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
this is what i could come up with:
import re
regex = r'[d]{1,3}, [d]{1,3}'
result =
with open('sample.txt') as f:
lines = f.readlines()
for line in lines:
match = re.findall(regex, line)
if match != :
splitted = match[0].split(',')
#the values are mapped to a list containing floating point numbers
mapped = list(map(float, splitted))
#and then are appended to a list that will contain all of
#the lines that have the numbers on it
result.append(mapped)
print(result)
#this is how you could access each line in result
for list in result:
print(list)
output
[[67.0, 78.0], [25.0, 18.0]] #result is a list containing all lines that have the pattern <number>, <number>
[67.0, 78.0] #the first line that matches the pattern
[25.0, 18.0] #the second one
this uses regular expressions to look for numbers up to 3 digits (but you can change that to whatever you want), matching the pattern <number>, <number>
if it matches the pattern, it splits the two numbers at the ,
creating a list containing those two values and appends them to the result list
Hope it helps.
Any questions feel free to ask.
Edit
im using this as a sample file to exemplify to you:
hi
hi
67, 78
hi again
25, 18
Thanks so much for your help! I am a very beginner at python so I don't really understand what's going on - even though you explained some of it! Do you mind explaining to me what re is, findall is, regex is, and finally the match != part. I know that this is basically the whole entire code, but I would really appreciate your help. Thanks again!
– EMCMAHE
Jan 3 at 14:36
1
“ re” is a built-in package for python that handles regular expressions. When “re” is imported, it works kind of a class, “findall” being one of the methods it has. “findall”, in this case takes two arguments, the regular expression “regex”, and a string (being whatever is on the line of the sample file)
– Gustavo Barros
Jan 3 at 14:54
1
"match" is the value that "re.findall" returns. If it matches the regex, its a list with all the matches in a line (in this case there is only one in one line). The "!=" is only an operator, it simply translates to "not equal to", and in this case, an empty list, meaning it will only append the matches that contains the regular expression.
– Gustavo Barros
Jan 3 at 15:03
1
I highlly suggest checking out the python documentation: docs.python.org/3, expecially the beginner's guide: docs.python.org/3.7/tutorial/index.html, it really helped me out some time ago. If you can't find what you're looking for in those pages, try searching in the index modules: docs.python.org/3.7/py-modindex.html
– Gustavo Barros
Jan 3 at 15:19
1
I made an edit for mapping the values from strings to floats using the map function
– Gustavo Barros
Jan 3 at 15:40
|
show 6 more comments
this is what i could come up with:
import re
regex = r'[d]{1,3}, [d]{1,3}'
result =
with open('sample.txt') as f:
lines = f.readlines()
for line in lines:
match = re.findall(regex, line)
if match != :
splitted = match[0].split(',')
#the values are mapped to a list containing floating point numbers
mapped = list(map(float, splitted))
#and then are appended to a list that will contain all of
#the lines that have the numbers on it
result.append(mapped)
print(result)
#this is how you could access each line in result
for list in result:
print(list)
output
[[67.0, 78.0], [25.0, 18.0]] #result is a list containing all lines that have the pattern <number>, <number>
[67.0, 78.0] #the first line that matches the pattern
[25.0, 18.0] #the second one
this uses regular expressions to look for numbers up to 3 digits (but you can change that to whatever you want), matching the pattern <number>, <number>
if it matches the pattern, it splits the two numbers at the ,
creating a list containing those two values and appends them to the result list
Hope it helps.
Any questions feel free to ask.
Edit
im using this as a sample file to exemplify to you:
hi
hi
67, 78
hi again
25, 18
Thanks so much for your help! I am a very beginner at python so I don't really understand what's going on - even though you explained some of it! Do you mind explaining to me what re is, findall is, regex is, and finally the match != part. I know that this is basically the whole entire code, but I would really appreciate your help. Thanks again!
– EMCMAHE
Jan 3 at 14:36
1
“ re” is a built-in package for python that handles regular expressions. When “re” is imported, it works kind of a class, “findall” being one of the methods it has. “findall”, in this case takes two arguments, the regular expression “regex”, and a string (being whatever is on the line of the sample file)
– Gustavo Barros
Jan 3 at 14:54
1
"match" is the value that "re.findall" returns. If it matches the regex, its a list with all the matches in a line (in this case there is only one in one line). The "!=" is only an operator, it simply translates to "not equal to", and in this case, an empty list, meaning it will only append the matches that contains the regular expression.
– Gustavo Barros
Jan 3 at 15:03
1
I highlly suggest checking out the python documentation: docs.python.org/3, expecially the beginner's guide: docs.python.org/3.7/tutorial/index.html, it really helped me out some time ago. If you can't find what you're looking for in those pages, try searching in the index modules: docs.python.org/3.7/py-modindex.html
– Gustavo Barros
Jan 3 at 15:19
1
I made an edit for mapping the values from strings to floats using the map function
– Gustavo Barros
Jan 3 at 15:40
|
show 6 more comments
this is what i could come up with:
import re
regex = r'[d]{1,3}, [d]{1,3}'
result =
with open('sample.txt') as f:
lines = f.readlines()
for line in lines:
match = re.findall(regex, line)
if match != :
splitted = match[0].split(',')
#the values are mapped to a list containing floating point numbers
mapped = list(map(float, splitted))
#and then are appended to a list that will contain all of
#the lines that have the numbers on it
result.append(mapped)
print(result)
#this is how you could access each line in result
for list in result:
print(list)
output
[[67.0, 78.0], [25.0, 18.0]] #result is a list containing all lines that have the pattern <number>, <number>
[67.0, 78.0] #the first line that matches the pattern
[25.0, 18.0] #the second one
this uses regular expressions to look for numbers up to 3 digits (but you can change that to whatever you want), matching the pattern <number>, <number>
if it matches the pattern, it splits the two numbers at the ,
creating a list containing those two values and appends them to the result list
Hope it helps.
Any questions feel free to ask.
Edit
im using this as a sample file to exemplify to you:
hi
hi
67, 78
hi again
25, 18
this is what i could come up with:
import re
regex = r'[d]{1,3}, [d]{1,3}'
result =
with open('sample.txt') as f:
lines = f.readlines()
for line in lines:
match = re.findall(regex, line)
if match != :
splitted = match[0].split(',')
#the values are mapped to a list containing floating point numbers
mapped = list(map(float, splitted))
#and then are appended to a list that will contain all of
#the lines that have the numbers on it
result.append(mapped)
print(result)
#this is how you could access each line in result
for list in result:
print(list)
output
[[67.0, 78.0], [25.0, 18.0]] #result is a list containing all lines that have the pattern <number>, <number>
[67.0, 78.0] #the first line that matches the pattern
[25.0, 18.0] #the second one
this uses regular expressions to look for numbers up to 3 digits (but you can change that to whatever you want), matching the pattern <number>, <number>
if it matches the pattern, it splits the two numbers at the ,
creating a list containing those two values and appends them to the result list
Hope it helps.
Any questions feel free to ask.
Edit
im using this as a sample file to exemplify to you:
hi
hi
67, 78
hi again
25, 18
edited Jan 6 at 23:48
answered Jan 3 at 5:06
Gustavo BarrosGustavo Barros
485
485
Thanks so much for your help! I am a very beginner at python so I don't really understand what's going on - even though you explained some of it! Do you mind explaining to me what re is, findall is, regex is, and finally the match != part. I know that this is basically the whole entire code, but I would really appreciate your help. Thanks again!
– EMCMAHE
Jan 3 at 14:36
1
“ re” is a built-in package for python that handles regular expressions. When “re” is imported, it works kind of a class, “findall” being one of the methods it has. “findall”, in this case takes two arguments, the regular expression “regex”, and a string (being whatever is on the line of the sample file)
– Gustavo Barros
Jan 3 at 14:54
1
"match" is the value that "re.findall" returns. If it matches the regex, its a list with all the matches in a line (in this case there is only one in one line). The "!=" is only an operator, it simply translates to "not equal to", and in this case, an empty list, meaning it will only append the matches that contains the regular expression.
– Gustavo Barros
Jan 3 at 15:03
1
I highlly suggest checking out the python documentation: docs.python.org/3, expecially the beginner's guide: docs.python.org/3.7/tutorial/index.html, it really helped me out some time ago. If you can't find what you're looking for in those pages, try searching in the index modules: docs.python.org/3.7/py-modindex.html
– Gustavo Barros
Jan 3 at 15:19
1
I made an edit for mapping the values from strings to floats using the map function
– Gustavo Barros
Jan 3 at 15:40
|
show 6 more comments
Thanks so much for your help! I am a very beginner at python so I don't really understand what's going on - even though you explained some of it! Do you mind explaining to me what re is, findall is, regex is, and finally the match != part. I know that this is basically the whole entire code, but I would really appreciate your help. Thanks again!
– EMCMAHE
Jan 3 at 14:36
1
“ re” is a built-in package for python that handles regular expressions. When “re” is imported, it works kind of a class, “findall” being one of the methods it has. “findall”, in this case takes two arguments, the regular expression “regex”, and a string (being whatever is on the line of the sample file)
– Gustavo Barros
Jan 3 at 14:54
1
"match" is the value that "re.findall" returns. If it matches the regex, its a list with all the matches in a line (in this case there is only one in one line). The "!=" is only an operator, it simply translates to "not equal to", and in this case, an empty list, meaning it will only append the matches that contains the regular expression.
– Gustavo Barros
Jan 3 at 15:03
1
I highlly suggest checking out the python documentation: docs.python.org/3, expecially the beginner's guide: docs.python.org/3.7/tutorial/index.html, it really helped me out some time ago. If you can't find what you're looking for in those pages, try searching in the index modules: docs.python.org/3.7/py-modindex.html
– Gustavo Barros
Jan 3 at 15:19
1
I made an edit for mapping the values from strings to floats using the map function
– Gustavo Barros
Jan 3 at 15:40
Thanks so much for your help! I am a very beginner at python so I don't really understand what's going on - even though you explained some of it! Do you mind explaining to me what re is, findall is, regex is, and finally the match != part. I know that this is basically the whole entire code, but I would really appreciate your help. Thanks again!
– EMCMAHE
Jan 3 at 14:36
Thanks so much for your help! I am a very beginner at python so I don't really understand what's going on - even though you explained some of it! Do you mind explaining to me what re is, findall is, regex is, and finally the match != part. I know that this is basically the whole entire code, but I would really appreciate your help. Thanks again!
– EMCMAHE
Jan 3 at 14:36
1
1
“ re” is a built-in package for python that handles regular expressions. When “re” is imported, it works kind of a class, “findall” being one of the methods it has. “findall”, in this case takes two arguments, the regular expression “regex”, and a string (being whatever is on the line of the sample file)
– Gustavo Barros
Jan 3 at 14:54
“ re” is a built-in package for python that handles regular expressions. When “re” is imported, it works kind of a class, “findall” being one of the methods it has. “findall”, in this case takes two arguments, the regular expression “regex”, and a string (being whatever is on the line of the sample file)
– Gustavo Barros
Jan 3 at 14:54
1
1
"match" is the value that "re.findall" returns. If it matches the regex, its a list with all the matches in a line (in this case there is only one in one line). The "!=" is only an operator, it simply translates to "not equal to", and in this case, an empty list, meaning it will only append the matches that contains the regular expression.
– Gustavo Barros
Jan 3 at 15:03
"match" is the value that "re.findall" returns. If it matches the regex, its a list with all the matches in a line (in this case there is only one in one line). The "!=" is only an operator, it simply translates to "not equal to", and in this case, an empty list, meaning it will only append the matches that contains the regular expression.
– Gustavo Barros
Jan 3 at 15:03
1
1
I highlly suggest checking out the python documentation: docs.python.org/3, expecially the beginner's guide: docs.python.org/3.7/tutorial/index.html, it really helped me out some time ago. If you can't find what you're looking for in those pages, try searching in the index modules: docs.python.org/3.7/py-modindex.html
– Gustavo Barros
Jan 3 at 15:19
I highlly suggest checking out the python documentation: docs.python.org/3, expecially the beginner's guide: docs.python.org/3.7/tutorial/index.html, it really helped me out some time ago. If you can't find what you're looking for in those pages, try searching in the index modules: docs.python.org/3.7/py-modindex.html
– Gustavo Barros
Jan 3 at 15:19
1
1
I made an edit for mapping the values from strings to floats using the map function
– Gustavo Barros
Jan 3 at 15:40
I made an edit for mapping the values from strings to floats using the map function
– Gustavo Barros
Jan 3 at 15:40
|
show 6 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54016234%2fpython-retrieving-two-separate-list-values-in-one-file-line%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Why do you have the argument
data_file
in the lineline = line.rstrip(data_file)
?– Xteven
Jan 3 at 4:15
@Xteven I thought that
line = line.rstrip()
needed to reference the file to strip the line– EMCMAHE
Jan 3 at 4:17
1
rstrip
is a method belonging to any string object in Python which accepts an optional argument of a string (which contains which characters to strip). More information here: docs.python.org/3/library/stdtypes.html#str.rstrip– Xteven
Jan 3 at 4:21
@Xteven thanks so much for your help! I have one more question if that's ok. When I removed the
data_file
from my code it said that theline = line.split('.')[1]
list index is out of range. Do u know how I can solve that?– EMCMAHE
Jan 3 at 4:26
In your sample file, none of the lines have the character
.
in them, which means thatline.split(".")
just returns a list of whatever was in that line because there is nothing to split by. The return list only has one item (whatever was in the line), so Python is complaining about trying to access the second item.– Xteven
Jan 3 at 4:29