Find the last row from a CSV input Python
I didn't really find an example related to my question as I don't know Pandas so I post it here. Let me know if this is not clear or already have been responded.
I have a CSV input which I import like this
def import_csv(csvfilename):
data =
with open(csvfilename, "r", encoding="utf-8", errors="ignore") as scraped:
reader = csv.reader(scraped, delimiter=',')
row_index = 0
for row in reader:
if row: # avoid blank lines
row_index += 1
columns = [str(row_index), row[0], row[1], row[2]]
data.append(columns)
return data
I index rows with input_rows (there is probably a better way for this?)
Input example :
[['1',
'[FirstValue]',
'FirstText',
'AB'],
[...]
['12',
"['LastValue']",
"LastText",
'YZ']]
I'm looking to get the last row of this insput list. Is there a simple way to do that without iterating over all the rows ?
Thank you !
python
add a comment |
I didn't really find an example related to my question as I don't know Pandas so I post it here. Let me know if this is not clear or already have been responded.
I have a CSV input which I import like this
def import_csv(csvfilename):
data =
with open(csvfilename, "r", encoding="utf-8", errors="ignore") as scraped:
reader = csv.reader(scraped, delimiter=',')
row_index = 0
for row in reader:
if row: # avoid blank lines
row_index += 1
columns = [str(row_index), row[0], row[1], row[2]]
data.append(columns)
return data
I index rows with input_rows (there is probably a better way for this?)
Input example :
[['1',
'[FirstValue]',
'FirstText',
'AB'],
[...]
['12',
"['LastValue']",
"LastText",
'YZ']]
I'm looking to get the last row of this insput list. Is there a simple way to do that without iterating over all the rows ?
Thank you !
python
Possible duplicate of How can I tail a log file in Python?
– Christian Sloper
Nov 26 '18 at 14:38
your input example looks like outputdata
list, doesn't it?
– Bogdan
Nov 26 '18 at 14:38
1
data[-1]
should do the trick.
– Idlehands
Nov 26 '18 at 14:39
add a comment |
I didn't really find an example related to my question as I don't know Pandas so I post it here. Let me know if this is not clear or already have been responded.
I have a CSV input which I import like this
def import_csv(csvfilename):
data =
with open(csvfilename, "r", encoding="utf-8", errors="ignore") as scraped:
reader = csv.reader(scraped, delimiter=',')
row_index = 0
for row in reader:
if row: # avoid blank lines
row_index += 1
columns = [str(row_index), row[0], row[1], row[2]]
data.append(columns)
return data
I index rows with input_rows (there is probably a better way for this?)
Input example :
[['1',
'[FirstValue]',
'FirstText',
'AB'],
[...]
['12',
"['LastValue']",
"LastText",
'YZ']]
I'm looking to get the last row of this insput list. Is there a simple way to do that without iterating over all the rows ?
Thank you !
python
I didn't really find an example related to my question as I don't know Pandas so I post it here. Let me know if this is not clear or already have been responded.
I have a CSV input which I import like this
def import_csv(csvfilename):
data =
with open(csvfilename, "r", encoding="utf-8", errors="ignore") as scraped:
reader = csv.reader(scraped, delimiter=',')
row_index = 0
for row in reader:
if row: # avoid blank lines
row_index += 1
columns = [str(row_index), row[0], row[1], row[2]]
data.append(columns)
return data
I index rows with input_rows (there is probably a better way for this?)
Input example :
[['1',
'[FirstValue]',
'FirstText',
'AB'],
[...]
['12',
"['LastValue']",
"LastText",
'YZ']]
I'm looking to get the last row of this insput list. Is there a simple way to do that without iterating over all the rows ?
Thank you !
python
python
edited Nov 26 '18 at 15:12
Charles Landau
2,4531216
2,4531216
asked Nov 26 '18 at 14:35
SidGabrielSidGabriel
736
736
Possible duplicate of How can I tail a log file in Python?
– Christian Sloper
Nov 26 '18 at 14:38
your input example looks like outputdata
list, doesn't it?
– Bogdan
Nov 26 '18 at 14:38
1
data[-1]
should do the trick.
– Idlehands
Nov 26 '18 at 14:39
add a comment |
Possible duplicate of How can I tail a log file in Python?
– Christian Sloper
Nov 26 '18 at 14:38
your input example looks like outputdata
list, doesn't it?
– Bogdan
Nov 26 '18 at 14:38
1
data[-1]
should do the trick.
– Idlehands
Nov 26 '18 at 14:39
Possible duplicate of How can I tail a log file in Python?
– Christian Sloper
Nov 26 '18 at 14:38
Possible duplicate of How can I tail a log file in Python?
– Christian Sloper
Nov 26 '18 at 14:38
your input example looks like output
data
list, doesn't it?– Bogdan
Nov 26 '18 at 14:38
your input example looks like output
data
list, doesn't it?– Bogdan
Nov 26 '18 at 14:38
1
1
data[-1]
should do the trick.– Idlehands
Nov 26 '18 at 14:39
data[-1]
should do the trick.– Idlehands
Nov 26 '18 at 14:39
add a comment |
3 Answers
3
active
oldest
votes
You can get the last element in an array like so:
some_list[-1]
In fact, you can do much more with this syntax. The some_list[-n]
syntax gets the nth-to-last element. So some_list[-1]
gets the last element, some_list[-2]
gets the second to last, etc
So in your case, it would be:
import csv
def import_csv(csvfilename):
data =
with open(csvfilename, "r", encoding="utf-8", errors="ignore") as scraped:
reader = csv.reader(scraped, delimiter=',')
for row in reader:
if row: # avoid blank lines
row_index += 1
columns = [str(row_index), row[0], row[1], row[2]]
data.append(columns)
return data
data = import_csv(file_name)
last_row = data[-1]
1
csv.reader
is an iterator and cannot be subscripted like that.
– Idlehands
Nov 26 '18 at 14:41
@Idlehands You're right, good catch. I've corrected the answer.
– Raoslaw Szamszur
Nov 26 '18 at 14:47
Odd that it locked my vote in even after your edit... If you would edit the answer again I would gladly retract my downvote.
– Idlehands
Nov 26 '18 at 14:50
1
Thank you very much for your answer ! I had a mistake while doing reader[-1] but not its ok! :)
– SidGabriel
Nov 26 '18 at 15:00
add a comment |
Python supports negative indexing.
your_list[-1] # Fetch the last value in your list.
Without knowing your use case and more about the data and how you typically access the data, it's hard to say what data structure would be better for you.
add a comment |
It's worth noting that csv.reader
is an Iterator and doesn't contain your data until iterated through. Same is true for the scraped
opened I/O Stream object which is also an iterator. The question to "Can I get the last row without iterating through the file/data" is unfortunately no, unless there is a specific stream point that you know can jump to (using scraped.seek()
), then you will not be able to get the very last row until all the data have been iterated through.
Once you have consumed all the data however, you can retrieve in your code with data[-1]
by means of negative indexing, i.e. returning the last row of the list
.
Here is a related question that might be of interest to you, but again, the answers all consume the data (reading the entirety as a list) prior to allowing the reverse()
operation, hence, all the data must be read through once at least.
Thanks for your explanation ! No there is no specific point so I'll be going with data[-1]
– SidGabriel
Nov 26 '18 at 15:02
add a comment |
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%2f53483389%2ffind-the-last-row-from-a-csv-input-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can get the last element in an array like so:
some_list[-1]
In fact, you can do much more with this syntax. The some_list[-n]
syntax gets the nth-to-last element. So some_list[-1]
gets the last element, some_list[-2]
gets the second to last, etc
So in your case, it would be:
import csv
def import_csv(csvfilename):
data =
with open(csvfilename, "r", encoding="utf-8", errors="ignore") as scraped:
reader = csv.reader(scraped, delimiter=',')
for row in reader:
if row: # avoid blank lines
row_index += 1
columns = [str(row_index), row[0], row[1], row[2]]
data.append(columns)
return data
data = import_csv(file_name)
last_row = data[-1]
1
csv.reader
is an iterator and cannot be subscripted like that.
– Idlehands
Nov 26 '18 at 14:41
@Idlehands You're right, good catch. I've corrected the answer.
– Raoslaw Szamszur
Nov 26 '18 at 14:47
Odd that it locked my vote in even after your edit... If you would edit the answer again I would gladly retract my downvote.
– Idlehands
Nov 26 '18 at 14:50
1
Thank you very much for your answer ! I had a mistake while doing reader[-1] but not its ok! :)
– SidGabriel
Nov 26 '18 at 15:00
add a comment |
You can get the last element in an array like so:
some_list[-1]
In fact, you can do much more with this syntax. The some_list[-n]
syntax gets the nth-to-last element. So some_list[-1]
gets the last element, some_list[-2]
gets the second to last, etc
So in your case, it would be:
import csv
def import_csv(csvfilename):
data =
with open(csvfilename, "r", encoding="utf-8", errors="ignore") as scraped:
reader = csv.reader(scraped, delimiter=',')
for row in reader:
if row: # avoid blank lines
row_index += 1
columns = [str(row_index), row[0], row[1], row[2]]
data.append(columns)
return data
data = import_csv(file_name)
last_row = data[-1]
1
csv.reader
is an iterator and cannot be subscripted like that.
– Idlehands
Nov 26 '18 at 14:41
@Idlehands You're right, good catch. I've corrected the answer.
– Raoslaw Szamszur
Nov 26 '18 at 14:47
Odd that it locked my vote in even after your edit... If you would edit the answer again I would gladly retract my downvote.
– Idlehands
Nov 26 '18 at 14:50
1
Thank you very much for your answer ! I had a mistake while doing reader[-1] but not its ok! :)
– SidGabriel
Nov 26 '18 at 15:00
add a comment |
You can get the last element in an array like so:
some_list[-1]
In fact, you can do much more with this syntax. The some_list[-n]
syntax gets the nth-to-last element. So some_list[-1]
gets the last element, some_list[-2]
gets the second to last, etc
So in your case, it would be:
import csv
def import_csv(csvfilename):
data =
with open(csvfilename, "r", encoding="utf-8", errors="ignore") as scraped:
reader = csv.reader(scraped, delimiter=',')
for row in reader:
if row: # avoid blank lines
row_index += 1
columns = [str(row_index), row[0], row[1], row[2]]
data.append(columns)
return data
data = import_csv(file_name)
last_row = data[-1]
You can get the last element in an array like so:
some_list[-1]
In fact, you can do much more with this syntax. The some_list[-n]
syntax gets the nth-to-last element. So some_list[-1]
gets the last element, some_list[-2]
gets the second to last, etc
So in your case, it would be:
import csv
def import_csv(csvfilename):
data =
with open(csvfilename, "r", encoding="utf-8", errors="ignore") as scraped:
reader = csv.reader(scraped, delimiter=',')
for row in reader:
if row: # avoid blank lines
row_index += 1
columns = [str(row_index), row[0], row[1], row[2]]
data.append(columns)
return data
data = import_csv(file_name)
last_row = data[-1]
edited Nov 26 '18 at 14:51
answered Nov 26 '18 at 14:38
Raoslaw SzamszurRaoslaw Szamszur
945515
945515
1
csv.reader
is an iterator and cannot be subscripted like that.
– Idlehands
Nov 26 '18 at 14:41
@Idlehands You're right, good catch. I've corrected the answer.
– Raoslaw Szamszur
Nov 26 '18 at 14:47
Odd that it locked my vote in even after your edit... If you would edit the answer again I would gladly retract my downvote.
– Idlehands
Nov 26 '18 at 14:50
1
Thank you very much for your answer ! I had a mistake while doing reader[-1] but not its ok! :)
– SidGabriel
Nov 26 '18 at 15:00
add a comment |
1
csv.reader
is an iterator and cannot be subscripted like that.
– Idlehands
Nov 26 '18 at 14:41
@Idlehands You're right, good catch. I've corrected the answer.
– Raoslaw Szamszur
Nov 26 '18 at 14:47
Odd that it locked my vote in even after your edit... If you would edit the answer again I would gladly retract my downvote.
– Idlehands
Nov 26 '18 at 14:50
1
Thank you very much for your answer ! I had a mistake while doing reader[-1] but not its ok! :)
– SidGabriel
Nov 26 '18 at 15:00
1
1
csv.reader
is an iterator and cannot be subscripted like that.– Idlehands
Nov 26 '18 at 14:41
csv.reader
is an iterator and cannot be subscripted like that.– Idlehands
Nov 26 '18 at 14:41
@Idlehands You're right, good catch. I've corrected the answer.
– Raoslaw Szamszur
Nov 26 '18 at 14:47
@Idlehands You're right, good catch. I've corrected the answer.
– Raoslaw Szamszur
Nov 26 '18 at 14:47
Odd that it locked my vote in even after your edit... If you would edit the answer again I would gladly retract my downvote.
– Idlehands
Nov 26 '18 at 14:50
Odd that it locked my vote in even after your edit... If you would edit the answer again I would gladly retract my downvote.
– Idlehands
Nov 26 '18 at 14:50
1
1
Thank you very much for your answer ! I had a mistake while doing reader[-1] but not its ok! :)
– SidGabriel
Nov 26 '18 at 15:00
Thank you very much for your answer ! I had a mistake while doing reader[-1] but not its ok! :)
– SidGabriel
Nov 26 '18 at 15:00
add a comment |
Python supports negative indexing.
your_list[-1] # Fetch the last value in your list.
Without knowing your use case and more about the data and how you typically access the data, it's hard to say what data structure would be better for you.
add a comment |
Python supports negative indexing.
your_list[-1] # Fetch the last value in your list.
Without knowing your use case and more about the data and how you typically access the data, it's hard to say what data structure would be better for you.
add a comment |
Python supports negative indexing.
your_list[-1] # Fetch the last value in your list.
Without knowing your use case and more about the data and how you typically access the data, it's hard to say what data structure would be better for you.
Python supports negative indexing.
your_list[-1] # Fetch the last value in your list.
Without knowing your use case and more about the data and how you typically access the data, it's hard to say what data structure would be better for you.
answered Nov 26 '18 at 14:38
Charles LandauCharles Landau
2,4531216
2,4531216
add a comment |
add a comment |
It's worth noting that csv.reader
is an Iterator and doesn't contain your data until iterated through. Same is true for the scraped
opened I/O Stream object which is also an iterator. The question to "Can I get the last row without iterating through the file/data" is unfortunately no, unless there is a specific stream point that you know can jump to (using scraped.seek()
), then you will not be able to get the very last row until all the data have been iterated through.
Once you have consumed all the data however, you can retrieve in your code with data[-1]
by means of negative indexing, i.e. returning the last row of the list
.
Here is a related question that might be of interest to you, but again, the answers all consume the data (reading the entirety as a list) prior to allowing the reverse()
operation, hence, all the data must be read through once at least.
Thanks for your explanation ! No there is no specific point so I'll be going with data[-1]
– SidGabriel
Nov 26 '18 at 15:02
add a comment |
It's worth noting that csv.reader
is an Iterator and doesn't contain your data until iterated through. Same is true for the scraped
opened I/O Stream object which is also an iterator. The question to "Can I get the last row without iterating through the file/data" is unfortunately no, unless there is a specific stream point that you know can jump to (using scraped.seek()
), then you will not be able to get the very last row until all the data have been iterated through.
Once you have consumed all the data however, you can retrieve in your code with data[-1]
by means of negative indexing, i.e. returning the last row of the list
.
Here is a related question that might be of interest to you, but again, the answers all consume the data (reading the entirety as a list) prior to allowing the reverse()
operation, hence, all the data must be read through once at least.
Thanks for your explanation ! No there is no specific point so I'll be going with data[-1]
– SidGabriel
Nov 26 '18 at 15:02
add a comment |
It's worth noting that csv.reader
is an Iterator and doesn't contain your data until iterated through. Same is true for the scraped
opened I/O Stream object which is also an iterator. The question to "Can I get the last row without iterating through the file/data" is unfortunately no, unless there is a specific stream point that you know can jump to (using scraped.seek()
), then you will not be able to get the very last row until all the data have been iterated through.
Once you have consumed all the data however, you can retrieve in your code with data[-1]
by means of negative indexing, i.e. returning the last row of the list
.
Here is a related question that might be of interest to you, but again, the answers all consume the data (reading the entirety as a list) prior to allowing the reverse()
operation, hence, all the data must be read through once at least.
It's worth noting that csv.reader
is an Iterator and doesn't contain your data until iterated through. Same is true for the scraped
opened I/O Stream object which is also an iterator. The question to "Can I get the last row without iterating through the file/data" is unfortunately no, unless there is a specific stream point that you know can jump to (using scraped.seek()
), then you will not be able to get the very last row until all the data have been iterated through.
Once you have consumed all the data however, you can retrieve in your code with data[-1]
by means of negative indexing, i.e. returning the last row of the list
.
Here is a related question that might be of interest to you, but again, the answers all consume the data (reading the entirety as a list) prior to allowing the reverse()
operation, hence, all the data must be read through once at least.
edited Nov 26 '18 at 14:59
answered Nov 26 '18 at 14:49
IdlehandsIdlehands
5,0901619
5,0901619
Thanks for your explanation ! No there is no specific point so I'll be going with data[-1]
– SidGabriel
Nov 26 '18 at 15:02
add a comment |
Thanks for your explanation ! No there is no specific point so I'll be going with data[-1]
– SidGabriel
Nov 26 '18 at 15:02
Thanks for your explanation ! No there is no specific point so I'll be going with data[-1]
– SidGabriel
Nov 26 '18 at 15:02
Thanks for your explanation ! No there is no specific point so I'll be going with data[-1]
– SidGabriel
Nov 26 '18 at 15:02
add a comment |
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%2f53483389%2ffind-the-last-row-from-a-csv-input-python%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
Possible duplicate of How can I tail a log file in Python?
– Christian Sloper
Nov 26 '18 at 14:38
your input example looks like output
data
list, doesn't it?– Bogdan
Nov 26 '18 at 14:38
1
data[-1]
should do the trick.– Idlehands
Nov 26 '18 at 14:39