Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将输入文件从Excel VBA发送到Raspberry Pi而不更改内容_Python_Excel_Vba_Raspberry Pi_Pscp - Fatal编程技术网

Python 将输入文件从Excel VBA发送到Raspberry Pi而不更改内容

Python 将输入文件从Excel VBA发送到Raspberry Pi而不更改内容,python,excel,vba,raspberry-pi,pscp,Python,Excel,Vba,Raspberry Pi,Pscp,我正在将一个文本文件从Excel发送到Raspberry Pi,Pi将在其中读取该文件并获取内部输入。输入只有一行,它是一个简单的数字,没有其他(40,38等) 当我运行此宏时,我可以在电脑中完美地看到输入文件,但当我在Pi中打开它时,它会发生更改,例如: 我的电脑中的输入文件是: 65 Raspberry中的输入文件为: ÿþ6 我如何才能确保这个号码被发送到Pi,就像它是。或者,如何将其解码为python脚本能够理解的内容。我不会对这个数字使用小数,而且它是否作为字符串发送也无关紧要,因

我正在将一个文本文件从Excel发送到Raspberry Pi,Pi将在其中读取该文件并获取内部输入。输入只有一行,它是一个简单的数字,没有其他(40,38等)

当我运行此宏时,我可以在电脑中完美地看到输入文件,但当我在Pi中打开它时,它会发生更改,例如:

我的电脑中的输入文件是:

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
pi@raspberrypi:~/Temp\u代码$python try.py
回溯(最近一次呼叫最后一次):
文件“try.py”,第6行,在
sleeptime=myfile.read()
文件“/usr/lib/python2.7/codecs.py”,第314行,解码中
(结果,消耗)=自身缓冲区解码(数据,自身错误,最终)
文件“/usr/lib/python2.7/encodings/utf_8_sig.py”,第66行,在缓冲区中解码
返回编解码器。utf_8_解码(输入、错误、最终)
UnicodeDecodeError:“utf8”编解码器无法解码位置0中的字节0xff:无效的开始字节

当Excel/VBA通过“CreateTextFile”创建Unicode文件时,它会将其保存为UTF-16编码。您看到的是BOM(字节顺序标记)

你可以改变

CreateTextFile("C:\Users\xx\Desktop\VBA\input.txt", True, True)

将另存为ASCII文本文件。或者您可以使用

open('input.txt', 'r', encoding='utf-16')

我刚刚编辑了我的帖子,并在我打开这个文件的python脚本中添加了行,编码是如何处理这些行的?将“open('input.txt',r')”更改为“open('input.txt',r',encoding='utf-8-sig')”再次编辑了我的帖子,在底部有一个小脚本和它给出的错误消息。我想我需要使用io.open来实现这一点,所以我这样做了。请尝试使用“encoding='utf-16',然后告诉我这是否有效。我将相应地编辑我的答案。是的,现在它与utf-16一起工作,非常感谢。编辑,我会接受答案。
open('input.txt', 'r', encoding='utf-16')