replace関数とstrip関数を使用して、open()関数のみのキャリッジリターンの問題を取り除きます。



Use Replace Strip Functions Remove Carriage Return Problem Open Function Alone



Linuxシステムでは、通常、ファイルの各行が読み取られてから、文字列にスペルミスされます。

次のようにシェルを使用します



#!/usr/bin/python #-*- coding: utf-8 -*- m=open('a.txt','r') for i in m: print('aaa%sbbbb' % i)

結果は次のとおりです。

#!/usr/bin/python #-*- coding: utf-8 -*- m=open('a.txt','r'): for i in m: m=i.replace(' ','') print('aaa%sbbbb' % i)

しかし、Pythonの実行結果を見てください



#!/usr/bin/python #-*- coding: utf-8 -*- m=open('a.txt','r') for i in m: m=i.strip() print('aaa%sbbbb' % i)

結果は次のとおりです。

#!/bin/bash for i in `cat a.txt`do echo 'aaa$ibbb' done

ここに n非表示

解決策1、置換機能を使用する



aaatestbbb

解決策2、ストリップ機能を使用する

|_+_|

結果は次のとおりです。

aaatest bbb


転載:https://blog.51cto.com/songhl/1727487