My current code, displays the results in both console and an output text file with the following statement
Ping ipping = new Ping();
PingReply replyab01 = ipping.Send(abilene01, 1000);
if (replyab01 != null)
{
ZplPrinterStatus.Items.Add("Abilene Primary(01) Printer Status:"+ replyab01.Status);
}
else
{
ZplPrinterStatus.Items.Add("Abilene Primary(01) Printer Status:" + "Error Printer Time out");
// ZplDownSatus.Visible = true;
}
My output on the text file is "Number of files processed within 512xB1 1 samples: 328"
But sometimes I do get the correct output"Number of files processed with 512± 1 samples: 3"
Console/interpreter output is fine with a print("") function where I always get ±. Being scouring and trying with encoding statements, any ideas?
答案 0 :(得分:1)
除非您明确要求源文件仅包含ASCII,或者您的编辑器不支持呈现此特定字符,否则只需在源代码中写下该字符:
fw.write("Number of files processed within 512±1 1 samples: "+str(count))
否则,使用utf8编码显式打开文件:
with open('file.txt', 'w', encoding='utf8') as fw:
fw.write("Number of files processed within 512\u00B1 1 samples: "+str(count))
答案 1 :(得分:1)
io.open
,声明编码(可以与源和控制台不同),并编写Unicode字符串。然后,如果您的控制台编码支持该字符(即使控制台的编码与源文件不同),它也会正确显示。文件将包含正确编码的字符。
示例(适用于Python 2和3):
#coding:utf8
from __future__ import unicode_literals,print_function
import io
count = 57
with io.open('out.txt','w',encoding='utf8') as fw:
fw.write("Number of files processed within 512±1 samples: {}".format(count))
print("Number of files processed within 512±1 samples: {}".format(count))
输出:
C:\temp>chcp # Console is a different encoding!
Active code page: 437
C:\temp>py -2 x.py # Python 2 displays correctly
Number of files processed within 512±1 samples: 57
C:\temp>py -3 x.py # Python 3 displays correctly
Number of files processed within 512±1 samples: 57
C:\temp>chcp 65001 # Change to output file encoding (UTF-8)
Active code page: 65001
C:\temp>type out.txt # Content of file is correct.
Number of files processed within 512±1 samples: 57
答案 2 :(得分:1)
首先,不要使用记事本,微软拒绝在Windows 10上添加UTF-8支持。
是的,建议在阅读或写入文件时强制使用UTF-8。
答案 3 :(得分:-1)
Try opening the file in write-binary mode by using # To split string :
# -----------------
string=ionworldionfriendsionPeople
echo "$string" | sed -e "s/\(.\)ion/\1\nion/g"
# To set in Array:
# ----------------
string=ionworldionfriendsionPeople
array=(`echo "$string" | sed -e "s/\(.\)ion/\1 ion/g"`)
# To check array content :
# ------------------------
echo ${array[*]}
.
If that doesn't work, try fw=open('file','wb')
to open in write mode with utf-8 encoding, which should solve your problem.