Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.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 Powershell生成的文本文件将行拆分为多个部分_Python_Powershell_Text - Fatal编程技术网

Python Powershell生成的文本文件将行拆分为多个部分

Python Powershell生成的文本文件将行拆分为多个部分,python,powershell,text,Python,Powershell,Text,我正在尝试从Python在Windows Powershell中运行命令: import subprocess, os, sys subprocess.check_call(['powershell.exe', 'Get-ItemProperty HKLM:/Software/Wow6432Node/Microsoft/Windows/CurrentVersion/Uninstall/* | Select-Object DisplayName, DisplayVersion, Publishe

我正在尝试从Python在Windows Powershell中运行命令:

import subprocess, os, sys

subprocess.check_call(['powershell.exe', 'Get-ItemProperty HKLM:/Software/Wow6432Node/Microsoft/Windows/CurrentVersion/Uninstall/* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate, InstallLocation > "C:/Program Files/softwarelist.txt"'])
但是,当使用UTF-16编码对创建的文件执行
sys.stdout.write()
-ing操作时(否则它会到处都有空格和兼容性问题),我会遇到以下问题:

DisplayName     : Adobe Flash Player 26 NPAPI

DisplayVersion  : 26.0.0.137

Publisher       : Adobe Systems Incorporated

InstallDate     : 

InstallLocation : 


DisplayName     : Adobe Flash Player 26 PPAPI

DisplayVersion  : 26.0.0.137

Publisher       : Adobe Systems Incorporated

InstallDate     : 

InstallLocation : 


DisplayName     : Agenda At Once 3.3.3

DisplayVersion  : 3.3.3

Publisher       : Dataland Software

InstallDate     : 20161106

InstallLocation : E:\Applications for Work\PIM and Projects\Agenda at Once 
                  PIM\Agenda at Once Components\Agenda At Once\
除了一件事之外,一切都如我所愿:最后一个
InstallLocation
字符串在文本文件中被分成两行(在“PIM”处)


有没有办法避免这种情况发生?

的可能重复。(感谢Ansgar Wiechers回答了评论中的问题。)

我会看看在PowerShell中使用
Export Csv
写入文件,以及Python中的
Csv
模块读取结果。为什么要使用Python(脚本)调用另一种脚本语言?用一种语言实现一切,让生活更轻松我同意,无论如何,试着使用
-Join“`r`n”
将它变成一个字符串而不是数组:
子进程。检查调用(['powershell.exe',”(Get-ItemProperty HKLM:/Software/Wow6432Node/Microsoft/Windows/CurrentVersion/Uninstall/*|选择对象DisplayName、DisplayVersion、Publisher、InstallDate、InstallLocation)-加入“`r`n”>“C:/Program Files/softwarelist.txt”])
我首先使用Powershell命令的唯一原因是生成一个包含(大多数)安装在我的计算机上的程序及其信息(我不知道Python中有任何这样的函数)。程序的其余部分是纯Python脚本编写的。我也尝试了你们在评论中提供的两种解决方案;两者都不起作用。我还可以补充一点,当我在Powershell窗口中使用相同的命令时,不会出现此问题。可能重复的可能重复的。