Python script on Unix writes on csv file but unreadable symbols at the end of the file in Windows 7
I am still learning python and I wrote script that is logging to cisco devices and run command and write to csv file and send as email attachment but when open the file on my Windows machine it has unreadable symbols:
If router is not reachable just writes file saying device down and it shows 0 as empty file in Unix but on Windows i see this:
Below is the script:
> def ssh_conn():
> user = 'xxxxxxx'
> passwd = 'xxxxxx'
> file2 = ("hostfile")
> hosts = open(file2,'r')
> for host in hosts:
> try:
> hostIp = host.split(',')[1]
> device = host.split(',')[0]
> session = paramiko.SSHClient()
> session.set_missing_host_key_policy(paramiko.AutoAddPolicy())
> session.connect(hostIp.strip(), username = user, password = passwd)
> connection = session.invoke_shell()
> connection.send("terminal width 0n")
> connection.send("sh archive config differences nvram:startup-config system:running-config | exc !|Diffn")
> time.sleep(1)
> output = connection.recv(5000)
> f = open(device +".csv", "w")
> f.write(output+'n')
> f.close()
> except:
> #f1 =codecs.open(device +"-DOWN.csv", "w", encoding="utf_16")
> f1 = open(device +"-DOWN.csv", "w")
> f1.close()
> pass
> files = glob.glob('*.csv')
> for file in files:
> if os.stat(file).st_size > 0:
> try:
> df=pd.read_csv(file,skiprows = 1).dropna()
> if len(df) <=2:
> os.remove(file)
> elif len(df) > 2:
> df=pd.read_csv(file, skiprows = 1, header=True).dropna()
> df.to_csv(file, index=False)
> else:
> print 'Empty File..passing'
> pass
> except ValueError:
> print 'Modified file'
> pass ssh_conn() print "Completed!"
I have tried using some encoders but same. Also was trying to write to txt file is the same. Hope you can provide a hint.
python-2.7 csv character-encoding
add a comment |
I am still learning python and I wrote script that is logging to cisco devices and run command and write to csv file and send as email attachment but when open the file on my Windows machine it has unreadable symbols:
If router is not reachable just writes file saying device down and it shows 0 as empty file in Unix but on Windows i see this:
Below is the script:
> def ssh_conn():
> user = 'xxxxxxx'
> passwd = 'xxxxxx'
> file2 = ("hostfile")
> hosts = open(file2,'r')
> for host in hosts:
> try:
> hostIp = host.split(',')[1]
> device = host.split(',')[0]
> session = paramiko.SSHClient()
> session.set_missing_host_key_policy(paramiko.AutoAddPolicy())
> session.connect(hostIp.strip(), username = user, password = passwd)
> connection = session.invoke_shell()
> connection.send("terminal width 0n")
> connection.send("sh archive config differences nvram:startup-config system:running-config | exc !|Diffn")
> time.sleep(1)
> output = connection.recv(5000)
> f = open(device +".csv", "w")
> f.write(output+'n')
> f.close()
> except:
> #f1 =codecs.open(device +"-DOWN.csv", "w", encoding="utf_16")
> f1 = open(device +"-DOWN.csv", "w")
> f1.close()
> pass
> files = glob.glob('*.csv')
> for file in files:
> if os.stat(file).st_size > 0:
> try:
> df=pd.read_csv(file,skiprows = 1).dropna()
> if len(df) <=2:
> os.remove(file)
> elif len(df) > 2:
> df=pd.read_csv(file, skiprows = 1, header=True).dropna()
> df.to_csv(file, index=False)
> else:
> print 'Empty File..passing'
> pass
> except ValueError:
> print 'Modified file'
> pass ssh_conn() print "Completed!"
I have tried using some encoders but same. Also was trying to write to txt file is the same. Hope you can provide a hint.
python-2.7 csv character-encoding
add a comment |
I am still learning python and I wrote script that is logging to cisco devices and run command and write to csv file and send as email attachment but when open the file on my Windows machine it has unreadable symbols:
If router is not reachable just writes file saying device down and it shows 0 as empty file in Unix but on Windows i see this:
Below is the script:
> def ssh_conn():
> user = 'xxxxxxx'
> passwd = 'xxxxxx'
> file2 = ("hostfile")
> hosts = open(file2,'r')
> for host in hosts:
> try:
> hostIp = host.split(',')[1]
> device = host.split(',')[0]
> session = paramiko.SSHClient()
> session.set_missing_host_key_policy(paramiko.AutoAddPolicy())
> session.connect(hostIp.strip(), username = user, password = passwd)
> connection = session.invoke_shell()
> connection.send("terminal width 0n")
> connection.send("sh archive config differences nvram:startup-config system:running-config | exc !|Diffn")
> time.sleep(1)
> output = connection.recv(5000)
> f = open(device +".csv", "w")
> f.write(output+'n')
> f.close()
> except:
> #f1 =codecs.open(device +"-DOWN.csv", "w", encoding="utf_16")
> f1 = open(device +"-DOWN.csv", "w")
> f1.close()
> pass
> files = glob.glob('*.csv')
> for file in files:
> if os.stat(file).st_size > 0:
> try:
> df=pd.read_csv(file,skiprows = 1).dropna()
> if len(df) <=2:
> os.remove(file)
> elif len(df) > 2:
> df=pd.read_csv(file, skiprows = 1, header=True).dropna()
> df.to_csv(file, index=False)
> else:
> print 'Empty File..passing'
> pass
> except ValueError:
> print 'Modified file'
> pass ssh_conn() print "Completed!"
I have tried using some encoders but same. Also was trying to write to txt file is the same. Hope you can provide a hint.
python-2.7 csv character-encoding
I am still learning python and I wrote script that is logging to cisco devices and run command and write to csv file and send as email attachment but when open the file on my Windows machine it has unreadable symbols:
If router is not reachable just writes file saying device down and it shows 0 as empty file in Unix but on Windows i see this:
Below is the script:
> def ssh_conn():
> user = 'xxxxxxx'
> passwd = 'xxxxxx'
> file2 = ("hostfile")
> hosts = open(file2,'r')
> for host in hosts:
> try:
> hostIp = host.split(',')[1]
> device = host.split(',')[0]
> session = paramiko.SSHClient()
> session.set_missing_host_key_policy(paramiko.AutoAddPolicy())
> session.connect(hostIp.strip(), username = user, password = passwd)
> connection = session.invoke_shell()
> connection.send("terminal width 0n")
> connection.send("sh archive config differences nvram:startup-config system:running-config | exc !|Diffn")
> time.sleep(1)
> output = connection.recv(5000)
> f = open(device +".csv", "w")
> f.write(output+'n')
> f.close()
> except:
> #f1 =codecs.open(device +"-DOWN.csv", "w", encoding="utf_16")
> f1 = open(device +"-DOWN.csv", "w")
> f1.close()
> pass
> files = glob.glob('*.csv')
> for file in files:
> if os.stat(file).st_size > 0:
> try:
> df=pd.read_csv(file,skiprows = 1).dropna()
> if len(df) <=2:
> os.remove(file)
> elif len(df) > 2:
> df=pd.read_csv(file, skiprows = 1, header=True).dropna()
> df.to_csv(file, index=False)
> else:
> print 'Empty File..passing'
> pass
> except ValueError:
> print 'Modified file'
> pass ssh_conn() print "Completed!"
I have tried using some encoders but same. Also was trying to write to txt file is the same. Hope you can provide a hint.
python-2.7 csv character-encoding
python-2.7 csv character-encoding
asked Nov 20 at 16:55
Ivan Madolev
179
179
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Actually the issues was with the script sending the email attachments which I fixed so closing the question .The script above is working fine
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%2f53397852%2fpython-script-on-unix-writes-on-csv-file-but-unreadable-symbols-at-the-end-of-th%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
Actually the issues was with the script sending the email attachments which I fixed so closing the question .The script above is working fine
add a comment |
Actually the issues was with the script sending the email attachments which I fixed so closing the question .The script above is working fine
add a comment |
Actually the issues was with the script sending the email attachments which I fixed so closing the question .The script above is working fine
Actually the issues was with the script sending the email attachments which I fixed so closing the question .The script above is working fine
answered Nov 22 at 8:19
Ivan Madolev
179
179
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53397852%2fpython-script-on-unix-writes-on-csv-file-but-unreadable-symbols-at-the-end-of-th%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