質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
コマンドプロンプト

コマンドプロンプト(cmd.exe)はMicrosoftによって提供されているコマンドラインインタプリタです。OS/2・Windows CE・Windows NTで使用可能です。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

2回答

2838閲覧

コマンドプロンプトで取得した内容に含まれる改行の削除

Poohtaro

総合スコア8

コマンドプロンプト

コマンドプロンプト(cmd.exe)はMicrosoftによって提供されているコマンドラインインタプリタです。OS/2・Windows CE・Windows NTで使用可能です。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2021/05/10 11:50

編集2021/05/10 11:56

前提・実現したいこと

pythonでコマンドプロンプトからGoogleChromeのバージョンを取得しようとしています。
取得まではできるのですが、取得した内容に多くの改行が入ったものになってしまうためこの改行を消したいです。

該当のソースコード

python

1import subprocess 2from subprocess import PIPE 3 4cmd=r'wmic datafile where name="C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" get Version /value' 5run=subprocess.run(cmd, shell=True, stdout=PIPE, stderr=PIPE, text=True) 6res = run.stdout 7res.strip(r"\n") 8print(res)

試したこと

listを作って何が入っているのか確認してみた。

python

1import subprocess 2from subprocess import PIPE 3 4A=[] 5cmd=r'wmic datafile where name="C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" get Version /value' 6run=subprocess.run(cmd, shell=True, stdout=PIPE, stderr=PIPE, text=True) 7res = run.stdout 8res.strip(r"\n") 9print(res) 10print(type(res)) 11print(len(res)) 12A.append(res) 13print(A) 14 15------------結果-------------- 16Version /value 17 18 19 20 21Version=90.0.4430.93 22 23 24 25 26 27 28 29 30<class 'str'> 3132 32['\n\n\n\nVersion=90.0.4430.93\n\n\n\n\n\n\n\n'] 33 34Process finished with exit code 0

strip(r"\n") → strip("")

python

1import subprocess 2from subprocess import PIPE 3 4cmd=r'wmic datafile where name="C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" get Version /value' 5run=subprocess.run(cmd, shell=True, stdout=PIPE, stderr=PIPE, text=True) 6res = run.stdout 7res.strip("") 8print(res) 9 10------------結果-------------- 11 12 13 14 15Version=90.0.4430.93 16 17 18 19 20 21 22 23 24 25Process finished with exit code 0

strip(r"\n") → replace(r"\n","")

python

1import subprocess 2from subprocess import PIPE 3 4cmd=r'wmic datafile where name="C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" get Version /value' 5run=subprocess.run(cmd, shell=True, stdout=PIPE, stderr=PIPE, text=True) 6res = run.stdout 7res.replace(r"\n","") 8print(res) 9 10------------結果-------------- 11 12 13 14 15Version=90.0.4430.93 16 17 18 19 20 21 22 23 24 25Process finished with exit code 0

strip(r"\n") → strip("'") strip(r"\n")

python

1import subprocess 2from subprocess import PIPE 3 4cmd=r'wmic datafile where name="C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" get Version /value' 5run=subprocess.run(cmd, shell=True, stdout=PIPE, stderr=PIPE, text=True) 6res = run.stdout 7res.strip("'") 8res.strip(r"\n") 9print(res) 10 11------------結果-------------- 12 13 14 15 16Version=90.0.4430.93 17 18 19 20 21 22 23 24 25 26Process finished with exit code 0

補足情報(FW/ツールのバージョンなど)

Python3.9
Windows10Home
PyCharm Community Edition 2021.1

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

「raw文字列」の意味がわかってないのでは?
r"\n"だと改行じゃなくて、「バックスラッシュとn」という2文字の文字列です。改行は"\n"です。

が、引数無しのstrip()で良いと思います。

投稿2021/05/10 12:29

otn

総合スコア84533

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

ベストアンサー

Python

1res = res.strip()

stripもreplaceも、結果を受け取らないと意味ないです。

Python

1>>> a = 'abc' 2>>> a.replace('a', '') 3'bc' 4>>> print(a) 5abc 6>>> 7>>> a = a.replace('a', '') 8>>> print(a) 9bc

投稿2021/05/10 12:13

編集2021/05/10 12:15
LouiS0616

総合スコア35660

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Poohtaro

2021/05/10 12:20 編集

回答ありがとうございます。 試してみましたが、やはり改行は消えませんでした。 追記 res=res.strip() にしたらできました。初歩的なことを忘れていました。 ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問