我正在将文本文件从Excel发送到Raspberry Pi,树莓派将在其中读取该文件并在其中输入内容。输入只是一行,它是一个简单的数字,仅此而已(40,38等)
当我运行此宏时,我可以在PC上完美地看到输入文件,但是当我在Pi中打开它时,例如:
我PC中的输入文件是:
65
Raspberry中的输入文件为:
ÿþ6
我如何确保此号码按原样发送给Pi。或者如何将其解码为我的python脚本可以理解的内容。我不会在此数字上使用小数,并且它是否作为字符串发送也没有关系,因为以后我可以在python代码中对其进行解析。
下面是我的Excel宏
Sub Send()
Dim sleeptime As String
sleeptime = InputBox("Duration between each reading in seconds?")
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim Fileout As Object
Set Fileout = fso.CreateTextFile("C:\Users\xx\Desktop\VBA\input.txt", True, True)
Fileout.Write sleeptime
Fileout.Close
Set fso = Nothing
Set Fileout = Nothing
Const cstrSftp As String = """C:\Program Files\PuTTY\pscp.exe"""
Dim strCommand As String
Dim pUser As String
Dim pPass As String
Dim pHost As String
Dim pFile As String
Dim pRemotePath As String
pUser = "pi" '//user on remote system
pPass = "xx" '//user's password on remote system
pHost = "192.168.x.xx" '//ip address of remote system
pFile = "C:\Users\xx\Desktop\VBA\input.txt" '//file to transfer
pRemotePath = "/home/pi/Temp_Codes" '//directory where file will be transferred to
strCommand = cstrSftp & " -sftp -l " & pUser & " -pw " & pPass & _
" " & pFile & " " & pHost & ":" & pRemotePath
Debug.Print strCommand
Shell strCommand, 0 ' vbHide
End Sub
这些是我使用此输入文件的Raspberry Pi脚本内的行。稍后,我在time.sleep中使用此sleeptime值。
with open('input.txt', 'r') as myfile:
sleeptime = myfile.read()
sleeptime = float(sleeptime.strip())
这是一个简单的代码,我正在运行以测试此编码内容:
#!/usr/bin/env python
import io
with io.open('input.txt', 'r', encoding='utf-8-sig') as myfile:
sleeptime = myfile.read()
sleeptime = float(sleeptime.strip())
这是我到目前为止遇到的错误。
pi@raspberrypi:~/Temp_Codes $ python try.py
Traceback (most recent call last):
File "try.py", line 6, in <module>
sleeptime = myfile.read()
File "/usr/lib/python2.7/codecs.py", line 314, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
File "/usr/lib/python2.7/encodings/utf_8_sig.py", line 66, in _buffer_decode
return codecs.utf_8_decode(input, errors, final)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xff in position 0: invalid start byte
答案 0 :(得分:1)
Excel / VBA通过“ CreateTextFile”创建Unicode文件时,会将其保存为UTF-16编码。您将看到的是BOM(字节顺序标记)。
您可以更改
export class TableFilteringExample implements OnInit
{
displayedColumns: string[] = ['position', 'name', 'weight', 'symbol'];
dataSource = new MatTableDataSource(ELEMENT_DATA);
positionFilter = new FormControl();
nameFilter = new FormControl();
filteredValues =
{
position: '',
name: '',
weight: '',
symbol: ''
};
ngOnInit()
{
this.positionFilter.valueChanges.subscribe((positionFilterValue) =>
{
this.filteredValues['position'] = positionFilterValue;
this.dataSource.filter = JSON.stringify(this.filteredValues);
});
this.nameFilter.valueChanges.subscribe((nameFilterValue) =>
{
this.filteredValues['name'] = nameFilterValue;
this.dataSource.filter = JSON.stringify(this.filteredValues);
});
this.dataSource.filterPredicate = this.customFilterPredicate();
}
applyFilter(filterValue: string)
{
this.dataSource.filter = filterValue.trim().toLowerCase();
this.dataSource.filter = filterValue;
}
customFilterPredicate()
{
const myFilterPredicate = function(data: PeriodicElement, filter: string): boolean
{
let searchString = JSON.parse(filter);
return data.position.toString().trim().indexOf(searchString.position) !== -1 && data.name.toString().trim().toLowerCase().indexOf(searchString.name)!== -1;
}
return myFilterPredicate;
}
}
到
CreateTextFile("C:\Users\xx\Desktop\VBA\input.txt", True, True)
这将另存为ASCII文本文件。或者,您可以使用
在Pi / Python方面进行更改CreateTextFile("C:\Users\xx\Desktop\VBA\input.txt", True, False)