get the last column in every file using open
how can i get the last column of an open file. I mean, it isn't the problem to do this with a panda dataframen.
I using for this problem the open methode to get only a few needed information out of different txt files. The mean problem is, that the result_metar is in different columns and isn't fixed on column 21.
for xx in range(len(fns_land)):
with open(fns_land[xx]) as infile:
for line in infile:
result_station.append(line.split(',')[0])
result_date.append(line.split(',')[1])
result_metar.append(line.split(',')[21])
is there a way to using iloc for example and change the fixed value 21?
The answer i found only show pandas solutions.
Thanks a lot.
Best
python pandas csv
add a comment |
how can i get the last column of an open file. I mean, it isn't the problem to do this with a panda dataframen.
I using for this problem the open methode to get only a few needed information out of different txt files. The mean problem is, that the result_metar is in different columns and isn't fixed on column 21.
for xx in range(len(fns_land)):
with open(fns_land[xx]) as infile:
for line in infile:
result_station.append(line.split(',')[0])
result_date.append(line.split(',')[1])
result_metar.append(line.split(',')[21])
is there a way to using iloc for example and change the fixed value 21?
The answer i found only show pandas solutions.
Thanks a lot.
Best
python pandas csv
It would be helpful if you can post a sample file
– Rahul Agarwal
Nov 14 '18 at 10:13
The complet file is to big. This is way i use a sequentiell reading solution.But i can show you how it looks like.
– S.Kociok
Nov 14 '18 at 10:15
Just a sample...1 or 2 line..so that folks can understand what is result_metar, result_date..and so on
– Rahul Agarwal
Nov 14 '18 at 10:16
DAOV,2015-01-01 00:00,33.80,30.20,86.49,0.00,0.00,0.00,30.39,M,6.21,M,M,M,M,M,M,M,M,M,M,DAOV 010000Z 00000KT CAVOK 01/M01 Q1029
– S.Kociok
Nov 14 '18 at 10:16
I use the station (column0], the date (column 1) and last but not least the last column.
– S.Kociok
Nov 14 '18 at 10:18
add a comment |
how can i get the last column of an open file. I mean, it isn't the problem to do this with a panda dataframen.
I using for this problem the open methode to get only a few needed information out of different txt files. The mean problem is, that the result_metar is in different columns and isn't fixed on column 21.
for xx in range(len(fns_land)):
with open(fns_land[xx]) as infile:
for line in infile:
result_station.append(line.split(',')[0])
result_date.append(line.split(',')[1])
result_metar.append(line.split(',')[21])
is there a way to using iloc for example and change the fixed value 21?
The answer i found only show pandas solutions.
Thanks a lot.
Best
python pandas csv
how can i get the last column of an open file. I mean, it isn't the problem to do this with a panda dataframen.
I using for this problem the open methode to get only a few needed information out of different txt files. The mean problem is, that the result_metar is in different columns and isn't fixed on column 21.
for xx in range(len(fns_land)):
with open(fns_land[xx]) as infile:
for line in infile:
result_station.append(line.split(',')[0])
result_date.append(line.split(',')[1])
result_metar.append(line.split(',')[21])
is there a way to using iloc for example and change the fixed value 21?
The answer i found only show pandas solutions.
Thanks a lot.
Best
python pandas csv
python pandas csv
asked Nov 14 '18 at 9:56
S.KociokS.Kociok
287
287
It would be helpful if you can post a sample file
– Rahul Agarwal
Nov 14 '18 at 10:13
The complet file is to big. This is way i use a sequentiell reading solution.But i can show you how it looks like.
– S.Kociok
Nov 14 '18 at 10:15
Just a sample...1 or 2 line..so that folks can understand what is result_metar, result_date..and so on
– Rahul Agarwal
Nov 14 '18 at 10:16
DAOV,2015-01-01 00:00,33.80,30.20,86.49,0.00,0.00,0.00,30.39,M,6.21,M,M,M,M,M,M,M,M,M,M,DAOV 010000Z 00000KT CAVOK 01/M01 Q1029
– S.Kociok
Nov 14 '18 at 10:16
I use the station (column0], the date (column 1) and last but not least the last column.
– S.Kociok
Nov 14 '18 at 10:18
add a comment |
It would be helpful if you can post a sample file
– Rahul Agarwal
Nov 14 '18 at 10:13
The complet file is to big. This is way i use a sequentiell reading solution.But i can show you how it looks like.
– S.Kociok
Nov 14 '18 at 10:15
Just a sample...1 or 2 line..so that folks can understand what is result_metar, result_date..and so on
– Rahul Agarwal
Nov 14 '18 at 10:16
DAOV,2015-01-01 00:00,33.80,30.20,86.49,0.00,0.00,0.00,30.39,M,6.21,M,M,M,M,M,M,M,M,M,M,DAOV 010000Z 00000KT CAVOK 01/M01 Q1029
– S.Kociok
Nov 14 '18 at 10:16
I use the station (column0], the date (column 1) and last but not least the last column.
– S.Kociok
Nov 14 '18 at 10:18
It would be helpful if you can post a sample file
– Rahul Agarwal
Nov 14 '18 at 10:13
It would be helpful if you can post a sample file
– Rahul Agarwal
Nov 14 '18 at 10:13
The complet file is to big. This is way i use a sequentiell reading solution.But i can show you how it looks like.
– S.Kociok
Nov 14 '18 at 10:15
The complet file is to big. This is way i use a sequentiell reading solution.But i can show you how it looks like.
– S.Kociok
Nov 14 '18 at 10:15
Just a sample...1 or 2 line..so that folks can understand what is result_metar, result_date..and so on
– Rahul Agarwal
Nov 14 '18 at 10:16
Just a sample...1 or 2 line..so that folks can understand what is result_metar, result_date..and so on
– Rahul Agarwal
Nov 14 '18 at 10:16
DAOV,2015-01-01 00:00,33.80,30.20,86.49,0.00,0.00,0.00,30.39,M,6.21,M,M,M,M,M,M,M,M,M,M,DAOV 010000Z 00000KT CAVOK 01/M01 Q1029
– S.Kociok
Nov 14 '18 at 10:16
DAOV,2015-01-01 00:00,33.80,30.20,86.49,0.00,0.00,0.00,30.39,M,6.21,M,M,M,M,M,M,M,M,M,M,DAOV 010000Z 00000KT CAVOK 01/M01 Q1029
– S.Kociok
Nov 14 '18 at 10:16
I use the station (column0], the date (column 1) and last but not least the last column.
– S.Kociok
Nov 14 '18 at 10:18
I use the station (column0], the date (column 1) and last but not least the last column.
– S.Kociok
Nov 14 '18 at 10:18
add a comment |
2 Answers
2
active
oldest
votes
Here you can return the last column of each open file regardless of the size of your file:
for xx in range(len(fns_land)):
with open(path) as infile:
lines = infile.readlines()
for line in lines:
result_station.append(line.split(',')[0])
result_date.append(line.split(',')[1])
result_metar.append(line.split(',')[-1])
1
I didn't get any values back. With [21] i get: DAOV 010000Z 00000KT...... with your solution the list is blank.
– S.Kociok
Nov 14 '18 at 10:21
1
The methodreadlines()
reads until EOF using readline() and returns a list containing the lines. In this method you have all the lines in your file once as a list. So you can apply split(',') on it.
– Ssein
Nov 14 '18 at 10:28
@S.Kociok maybe my answer works for you?
– Harly H.
Nov 14 '18 at 10:29
Ah now i understand it.
– S.Kociok
Nov 14 '18 at 10:29
Sorry I have to ask.... Now i only get on line of the file. If i switch back to 21 it runs and i get a list... if i use youre line only one line is append... Can you help me a second time?
– S.Kociok
Nov 14 '18 at 11:14
|
show 7 more comments
First I want to point out that you're calling the line.split() multiple times, which is in-optimal. Instead you could split the line once by assigning the split result list to variable. Then it's just a matter of picking the items from the list.
But... In reality I'd go with this:
for filename in fns_land:
with open(filename) as infile:
last_line = infile.readlines()[-1]
last_column = last_line.split(',')[-1]
result_metar.append(last_column)
"""
# alternatively 3 lines above could be just be one command, such as
result_metar.append( (infile.readlines()[-1]).split(',')[-1] )
"""
In your case i get a empty result_metar but i don't now way :D
– S.Kociok
Nov 14 '18 at 10:55
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%2f53297377%2fget-the-last-column-in-every-file-using-open%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Here you can return the last column of each open file regardless of the size of your file:
for xx in range(len(fns_land)):
with open(path) as infile:
lines = infile.readlines()
for line in lines:
result_station.append(line.split(',')[0])
result_date.append(line.split(',')[1])
result_metar.append(line.split(',')[-1])
1
I didn't get any values back. With [21] i get: DAOV 010000Z 00000KT...... with your solution the list is blank.
– S.Kociok
Nov 14 '18 at 10:21
1
The methodreadlines()
reads until EOF using readline() and returns a list containing the lines. In this method you have all the lines in your file once as a list. So you can apply split(',') on it.
– Ssein
Nov 14 '18 at 10:28
@S.Kociok maybe my answer works for you?
– Harly H.
Nov 14 '18 at 10:29
Ah now i understand it.
– S.Kociok
Nov 14 '18 at 10:29
Sorry I have to ask.... Now i only get on line of the file. If i switch back to 21 it runs and i get a list... if i use youre line only one line is append... Can you help me a second time?
– S.Kociok
Nov 14 '18 at 11:14
|
show 7 more comments
Here you can return the last column of each open file regardless of the size of your file:
for xx in range(len(fns_land)):
with open(path) as infile:
lines = infile.readlines()
for line in lines:
result_station.append(line.split(',')[0])
result_date.append(line.split(',')[1])
result_metar.append(line.split(',')[-1])
1
I didn't get any values back. With [21] i get: DAOV 010000Z 00000KT...... with your solution the list is blank.
– S.Kociok
Nov 14 '18 at 10:21
1
The methodreadlines()
reads until EOF using readline() and returns a list containing the lines. In this method you have all the lines in your file once as a list. So you can apply split(',') on it.
– Ssein
Nov 14 '18 at 10:28
@S.Kociok maybe my answer works for you?
– Harly H.
Nov 14 '18 at 10:29
Ah now i understand it.
– S.Kociok
Nov 14 '18 at 10:29
Sorry I have to ask.... Now i only get on line of the file. If i switch back to 21 it runs and i get a list... if i use youre line only one line is append... Can you help me a second time?
– S.Kociok
Nov 14 '18 at 11:14
|
show 7 more comments
Here you can return the last column of each open file regardless of the size of your file:
for xx in range(len(fns_land)):
with open(path) as infile:
lines = infile.readlines()
for line in lines:
result_station.append(line.split(',')[0])
result_date.append(line.split(',')[1])
result_metar.append(line.split(',')[-1])
Here you can return the last column of each open file regardless of the size of your file:
for xx in range(len(fns_land)):
with open(path) as infile:
lines = infile.readlines()
for line in lines:
result_station.append(line.split(',')[0])
result_date.append(line.split(',')[1])
result_metar.append(line.split(',')[-1])
edited Nov 14 '18 at 12:26
answered Nov 14 '18 at 10:16
SseinSsein
1,0241921
1,0241921
1
I didn't get any values back. With [21] i get: DAOV 010000Z 00000KT...... with your solution the list is blank.
– S.Kociok
Nov 14 '18 at 10:21
1
The methodreadlines()
reads until EOF using readline() and returns a list containing the lines. In this method you have all the lines in your file once as a list. So you can apply split(',') on it.
– Ssein
Nov 14 '18 at 10:28
@S.Kociok maybe my answer works for you?
– Harly H.
Nov 14 '18 at 10:29
Ah now i understand it.
– S.Kociok
Nov 14 '18 at 10:29
Sorry I have to ask.... Now i only get on line of the file. If i switch back to 21 it runs and i get a list... if i use youre line only one line is append... Can you help me a second time?
– S.Kociok
Nov 14 '18 at 11:14
|
show 7 more comments
1
I didn't get any values back. With [21] i get: DAOV 010000Z 00000KT...... with your solution the list is blank.
– S.Kociok
Nov 14 '18 at 10:21
1
The methodreadlines()
reads until EOF using readline() and returns a list containing the lines. In this method you have all the lines in your file once as a list. So you can apply split(',') on it.
– Ssein
Nov 14 '18 at 10:28
@S.Kociok maybe my answer works for you?
– Harly H.
Nov 14 '18 at 10:29
Ah now i understand it.
– S.Kociok
Nov 14 '18 at 10:29
Sorry I have to ask.... Now i only get on line of the file. If i switch back to 21 it runs and i get a list... if i use youre line only one line is append... Can you help me a second time?
– S.Kociok
Nov 14 '18 at 11:14
1
1
I didn't get any values back. With [21] i get: DAOV 010000Z 00000KT...... with your solution the list is blank.
– S.Kociok
Nov 14 '18 at 10:21
I didn't get any values back. With [21] i get: DAOV 010000Z 00000KT...... with your solution the list is blank.
– S.Kociok
Nov 14 '18 at 10:21
1
1
The method
readlines()
reads until EOF using readline() and returns a list containing the lines. In this method you have all the lines in your file once as a list. So you can apply split(',') on it.– Ssein
Nov 14 '18 at 10:28
The method
readlines()
reads until EOF using readline() and returns a list containing the lines. In this method you have all the lines in your file once as a list. So you can apply split(',') on it.– Ssein
Nov 14 '18 at 10:28
@S.Kociok maybe my answer works for you?
– Harly H.
Nov 14 '18 at 10:29
@S.Kociok maybe my answer works for you?
– Harly H.
Nov 14 '18 at 10:29
Ah now i understand it.
– S.Kociok
Nov 14 '18 at 10:29
Ah now i understand it.
– S.Kociok
Nov 14 '18 at 10:29
Sorry I have to ask.... Now i only get on line of the file. If i switch back to 21 it runs and i get a list... if i use youre line only one line is append... Can you help me a second time?
– S.Kociok
Nov 14 '18 at 11:14
Sorry I have to ask.... Now i only get on line of the file. If i switch back to 21 it runs and i get a list... if i use youre line only one line is append... Can you help me a second time?
– S.Kociok
Nov 14 '18 at 11:14
|
show 7 more comments
First I want to point out that you're calling the line.split() multiple times, which is in-optimal. Instead you could split the line once by assigning the split result list to variable. Then it's just a matter of picking the items from the list.
But... In reality I'd go with this:
for filename in fns_land:
with open(filename) as infile:
last_line = infile.readlines()[-1]
last_column = last_line.split(',')[-1]
result_metar.append(last_column)
"""
# alternatively 3 lines above could be just be one command, such as
result_metar.append( (infile.readlines()[-1]).split(',')[-1] )
"""
In your case i get a empty result_metar but i don't now way :D
– S.Kociok
Nov 14 '18 at 10:55
add a comment |
First I want to point out that you're calling the line.split() multiple times, which is in-optimal. Instead you could split the line once by assigning the split result list to variable. Then it's just a matter of picking the items from the list.
But... In reality I'd go with this:
for filename in fns_land:
with open(filename) as infile:
last_line = infile.readlines()[-1]
last_column = last_line.split(',')[-1]
result_metar.append(last_column)
"""
# alternatively 3 lines above could be just be one command, such as
result_metar.append( (infile.readlines()[-1]).split(',')[-1] )
"""
In your case i get a empty result_metar but i don't now way :D
– S.Kociok
Nov 14 '18 at 10:55
add a comment |
First I want to point out that you're calling the line.split() multiple times, which is in-optimal. Instead you could split the line once by assigning the split result list to variable. Then it's just a matter of picking the items from the list.
But... In reality I'd go with this:
for filename in fns_land:
with open(filename) as infile:
last_line = infile.readlines()[-1]
last_column = last_line.split(',')[-1]
result_metar.append(last_column)
"""
# alternatively 3 lines above could be just be one command, such as
result_metar.append( (infile.readlines()[-1]).split(',')[-1] )
"""
First I want to point out that you're calling the line.split() multiple times, which is in-optimal. Instead you could split the line once by assigning the split result list to variable. Then it's just a matter of picking the items from the list.
But... In reality I'd go with this:
for filename in fns_land:
with open(filename) as infile:
last_line = infile.readlines()[-1]
last_column = last_line.split(',')[-1]
result_metar.append(last_column)
"""
# alternatively 3 lines above could be just be one command, such as
result_metar.append( (infile.readlines()[-1]).split(',')[-1] )
"""
edited Nov 14 '18 at 10:32
answered Nov 14 '18 at 10:27
Harly H.Harly H.
44527
44527
In your case i get a empty result_metar but i don't now way :D
– S.Kociok
Nov 14 '18 at 10:55
add a comment |
In your case i get a empty result_metar but i don't now way :D
– S.Kociok
Nov 14 '18 at 10:55
In your case i get a empty result_metar but i don't now way :D
– S.Kociok
Nov 14 '18 at 10:55
In your case i get a empty result_metar but i don't now way :D
– S.Kociok
Nov 14 '18 at 10:55
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%2f53297377%2fget-the-last-column-in-every-file-using-open%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
It would be helpful if you can post a sample file
– Rahul Agarwal
Nov 14 '18 at 10:13
The complet file is to big. This is way i use a sequentiell reading solution.But i can show you how it looks like.
– S.Kociok
Nov 14 '18 at 10:15
Just a sample...1 or 2 line..so that folks can understand what is result_metar, result_date..and so on
– Rahul Agarwal
Nov 14 '18 at 10:16
DAOV,2015-01-01 00:00,33.80,30.20,86.49,0.00,0.00,0.00,30.39,M,6.21,M,M,M,M,M,M,M,M,M,M,DAOV 010000Z 00000KT CAVOK 01/M01 Q1029
– S.Kociok
Nov 14 '18 at 10:16
I use the station (column0], the date (column 1) and last but not least the last column.
– S.Kociok
Nov 14 '18 at 10:18