Open csv file with Pandas and delete if has only 1 row












0















I have a task to create a script to ssh to list of 10 cisco routers weekly and check for config changes and send notification. So i have in place the script that logs and run the command and send it to csv. I have modified so if there is not changes all I have in the csv will be for example:
rtr0003# -which is the router name only. If there will be conf change the excel will have inside for example:



enter image description here



My question is how to run pandas to open each file and if it sees only one line/row to delete the excel file and if more lines to skip it.



This is how i write the files:



files = glob.glob('*.csv')
for file in files:
df=pd.read_csv(file)
df=df.dropna()
df.to_csv(file,index=False)
df1=pd.read_csv(file,skiprows = 2)
#df1=df1.drop(df1.tail(1))
df1.to_csv(file,index=False)









share|improve this question




















  • 1





    Why use python / pandas for this at all? You could just do it in bash source

    – forgetso
    Nov 14 '18 at 10:52











  • @forgetso, But OP wants this in python as a choice :-)

    – pygo
    Nov 14 '18 at 10:59











  • all at once-using paramiko to ssh and run the command , then pandas to create and filter the csv and then send an email.Seems all easier then mixing bash , python etc.

    – Ivan Madolev
    Nov 14 '18 at 14:02
















0















I have a task to create a script to ssh to list of 10 cisco routers weekly and check for config changes and send notification. So i have in place the script that logs and run the command and send it to csv. I have modified so if there is not changes all I have in the csv will be for example:
rtr0003# -which is the router name only. If there will be conf change the excel will have inside for example:



enter image description here



My question is how to run pandas to open each file and if it sees only one line/row to delete the excel file and if more lines to skip it.



This is how i write the files:



files = glob.glob('*.csv')
for file in files:
df=pd.read_csv(file)
df=df.dropna()
df.to_csv(file,index=False)
df1=pd.read_csv(file,skiprows = 2)
#df1=df1.drop(df1.tail(1))
df1.to_csv(file,index=False)









share|improve this question




















  • 1





    Why use python / pandas for this at all? You could just do it in bash source

    – forgetso
    Nov 14 '18 at 10:52











  • @forgetso, But OP wants this in python as a choice :-)

    – pygo
    Nov 14 '18 at 10:59











  • all at once-using paramiko to ssh and run the command , then pandas to create and filter the csv and then send an email.Seems all easier then mixing bash , python etc.

    – Ivan Madolev
    Nov 14 '18 at 14:02














0












0








0








I have a task to create a script to ssh to list of 10 cisco routers weekly and check for config changes and send notification. So i have in place the script that logs and run the command and send it to csv. I have modified so if there is not changes all I have in the csv will be for example:
rtr0003# -which is the router name only. If there will be conf change the excel will have inside for example:



enter image description here



My question is how to run pandas to open each file and if it sees only one line/row to delete the excel file and if more lines to skip it.



This is how i write the files:



files = glob.glob('*.csv')
for file in files:
df=pd.read_csv(file)
df=df.dropna()
df.to_csv(file,index=False)
df1=pd.read_csv(file,skiprows = 2)
#df1=df1.drop(df1.tail(1))
df1.to_csv(file,index=False)









share|improve this question
















I have a task to create a script to ssh to list of 10 cisco routers weekly and check for config changes and send notification. So i have in place the script that logs and run the command and send it to csv. I have modified so if there is not changes all I have in the csv will be for example:
rtr0003# -which is the router name only. If there will be conf change the excel will have inside for example:



enter image description here



My question is how to run pandas to open each file and if it sees only one line/row to delete the excel file and if more lines to skip it.



This is how i write the files:



files = glob.glob('*.csv')
for file in files:
df=pd.read_csv(file)
df=df.dropna()
df.to_csv(file,index=False)
df1=pd.read_csv(file,skiprows = 2)
#df1=df1.drop(df1.tail(1))
df1.to_csv(file,index=False)






python pandas opencsv






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 12:36









Aqueous Carlos

352314




352314










asked Nov 14 '18 at 10:45









Ivan MadolevIvan Madolev

179




179








  • 1





    Why use python / pandas for this at all? You could just do it in bash source

    – forgetso
    Nov 14 '18 at 10:52











  • @forgetso, But OP wants this in python as a choice :-)

    – pygo
    Nov 14 '18 at 10:59











  • all at once-using paramiko to ssh and run the command , then pandas to create and filter the csv and then send an email.Seems all easier then mixing bash , python etc.

    – Ivan Madolev
    Nov 14 '18 at 14:02














  • 1





    Why use python / pandas for this at all? You could just do it in bash source

    – forgetso
    Nov 14 '18 at 10:52











  • @forgetso, But OP wants this in python as a choice :-)

    – pygo
    Nov 14 '18 at 10:59











  • all at once-using paramiko to ssh and run the command , then pandas to create and filter the csv and then send an email.Seems all easier then mixing bash , python etc.

    – Ivan Madolev
    Nov 14 '18 at 14:02








1




1





Why use python / pandas for this at all? You could just do it in bash source

– forgetso
Nov 14 '18 at 10:52





Why use python / pandas for this at all? You could just do it in bash source

– forgetso
Nov 14 '18 at 10:52













@forgetso, But OP wants this in python as a choice :-)

– pygo
Nov 14 '18 at 10:59





@forgetso, But OP wants this in python as a choice :-)

– pygo
Nov 14 '18 at 10:59













all at once-using paramiko to ssh and run the command , then pandas to create and filter the csv and then send an email.Seems all easier then mixing bash , python etc.

– Ivan Madolev
Nov 14 '18 at 14:02





all at once-using paramiko to ssh and run the command , then pandas to create and filter the csv and then send an email.Seems all easier then mixing bash , python etc.

– Ivan Madolev
Nov 14 '18 at 14:02












2 Answers
2






active

oldest

votes


















1














import os    
import glob
import csv

files = glob.glob('*.csv')

for file in files:
with open(file,"r") as f:
reader = csv.reader(f,delimiter = ",")
data = list(reader)
row_count = len(data)

if row_count == 1:
os.remove(file)





share|improve this answer


























  • Thanks Karl! will check and reply!

    – Ivan Madolev
    Nov 14 '18 at 12:20



















1














Here is a solution using pandas:



import pandas as pd
import glob
import os

csv_files = glob.glob('*.csv')
for file in csv_files:
df_file = pd.read_csv(file, low_memory = False)
if len(df_file) == 1:
os.remove(file)


If you are using excel files, change



glob.glob('*.csv')  


to



glob.glob('*.xlsx')


and



pd.read_csv(file, low_memory = False)


to



pd.read_excel(file)





share|improve this answer
























  • Thanks Jorge for the fast reply! Will test and get back to you

    – Ivan Madolev
    Nov 14 '18 at 12:20






  • 1





    Worked like charm! Thank you Jorge! accepting the answer

    – Ivan Madolev
    Nov 14 '18 at 13:56











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53298352%2fopen-csv-file-with-pandas-and-delete-if-has-only-1-row%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









1














import os    
import glob
import csv

files = glob.glob('*.csv')

for file in files:
with open(file,"r") as f:
reader = csv.reader(f,delimiter = ",")
data = list(reader)
row_count = len(data)

if row_count == 1:
os.remove(file)





share|improve this answer


























  • Thanks Karl! will check and reply!

    – Ivan Madolev
    Nov 14 '18 at 12:20
















1














import os    
import glob
import csv

files = glob.glob('*.csv')

for file in files:
with open(file,"r") as f:
reader = csv.reader(f,delimiter = ",")
data = list(reader)
row_count = len(data)

if row_count == 1:
os.remove(file)





share|improve this answer


























  • Thanks Karl! will check and reply!

    – Ivan Madolev
    Nov 14 '18 at 12:20














1












1








1







import os    
import glob
import csv

files = glob.glob('*.csv')

for file in files:
with open(file,"r") as f:
reader = csv.reader(f,delimiter = ",")
data = list(reader)
row_count = len(data)

if row_count == 1:
os.remove(file)





share|improve this answer















import os    
import glob
import csv

files = glob.glob('*.csv')

for file in files:
with open(file,"r") as f:
reader = csv.reader(f,delimiter = ",")
data = list(reader)
row_count = len(data)

if row_count == 1:
os.remove(file)






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 11:11

























answered Nov 14 '18 at 11:05









KarlKarl

2,39643055




2,39643055













  • Thanks Karl! will check and reply!

    – Ivan Madolev
    Nov 14 '18 at 12:20



















  • Thanks Karl! will check and reply!

    – Ivan Madolev
    Nov 14 '18 at 12:20

















Thanks Karl! will check and reply!

– Ivan Madolev
Nov 14 '18 at 12:20





Thanks Karl! will check and reply!

– Ivan Madolev
Nov 14 '18 at 12:20













1














Here is a solution using pandas:



import pandas as pd
import glob
import os

csv_files = glob.glob('*.csv')
for file in csv_files:
df_file = pd.read_csv(file, low_memory = False)
if len(df_file) == 1:
os.remove(file)


If you are using excel files, change



glob.glob('*.csv')  


to



glob.glob('*.xlsx')


and



pd.read_csv(file, low_memory = False)


to



pd.read_excel(file)





share|improve this answer
























  • Thanks Jorge for the fast reply! Will test and get back to you

    – Ivan Madolev
    Nov 14 '18 at 12:20






  • 1





    Worked like charm! Thank you Jorge! accepting the answer

    – Ivan Madolev
    Nov 14 '18 at 13:56
















1














Here is a solution using pandas:



import pandas as pd
import glob
import os

csv_files = glob.glob('*.csv')
for file in csv_files:
df_file = pd.read_csv(file, low_memory = False)
if len(df_file) == 1:
os.remove(file)


If you are using excel files, change



glob.glob('*.csv')  


to



glob.glob('*.xlsx')


and



pd.read_csv(file, low_memory = False)


to



pd.read_excel(file)





share|improve this answer
























  • Thanks Jorge for the fast reply! Will test and get back to you

    – Ivan Madolev
    Nov 14 '18 at 12:20






  • 1





    Worked like charm! Thank you Jorge! accepting the answer

    – Ivan Madolev
    Nov 14 '18 at 13:56














1












1








1







Here is a solution using pandas:



import pandas as pd
import glob
import os

csv_files = glob.glob('*.csv')
for file in csv_files:
df_file = pd.read_csv(file, low_memory = False)
if len(df_file) == 1:
os.remove(file)


If you are using excel files, change



glob.glob('*.csv')  


to



glob.glob('*.xlsx')


and



pd.read_csv(file, low_memory = False)


to



pd.read_excel(file)





share|improve this answer













Here is a solution using pandas:



import pandas as pd
import glob
import os

csv_files = glob.glob('*.csv')
for file in csv_files:
df_file = pd.read_csv(file, low_memory = False)
if len(df_file) == 1:
os.remove(file)


If you are using excel files, change



glob.glob('*.csv')  


to



glob.glob('*.xlsx')


and



pd.read_csv(file, low_memory = False)


to



pd.read_excel(file)






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 11:27









JorgeJorge

1,2631921




1,2631921













  • Thanks Jorge for the fast reply! Will test and get back to you

    – Ivan Madolev
    Nov 14 '18 at 12:20






  • 1





    Worked like charm! Thank you Jorge! accepting the answer

    – Ivan Madolev
    Nov 14 '18 at 13:56



















  • Thanks Jorge for the fast reply! Will test and get back to you

    – Ivan Madolev
    Nov 14 '18 at 12:20






  • 1





    Worked like charm! Thank you Jorge! accepting the answer

    – Ivan Madolev
    Nov 14 '18 at 13:56

















Thanks Jorge for the fast reply! Will test and get back to you

– Ivan Madolev
Nov 14 '18 at 12:20





Thanks Jorge for the fast reply! Will test and get back to you

– Ivan Madolev
Nov 14 '18 at 12:20




1




1





Worked like charm! Thank you Jorge! accepting the answer

– Ivan Madolev
Nov 14 '18 at 13:56





Worked like charm! Thank you Jorge! accepting the answer

– Ivan Madolev
Nov 14 '18 at 13:56


















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53298352%2fopen-csv-file-with-pandas-and-delete-if-has-only-1-row%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Bressuire

Vorschmack

Quarantine