python中的input()和\n字符

python中的input()和\n字符,python,python-3.x,Python,Python 3.x,我试图用input()查找并替换多个文件中的几行纯文本,但当我输入“\n”字符表示新行字符在文本中的位置时,它找不到,也不替换 我试图使用原始字符串,但无法让它们工作 这是正则表达式的工作吗 python 3.7 import os import re import time start = time.time() # enter path and check input for standard format scan_folder = input('Enter the absolute

我试图用input()查找并替换多个文件中的几行纯文本,但当我输入“\n”字符表示新行字符在文本中的位置时,它找不到,也不替换

我试图使用原始字符串,但无法让它们工作

这是正则表达式的工作吗

python 3.7

import os
import re
import time

start = time.time()
# enter path and check input for standard format
scan_folder = input('Enter the absolute path to scan:\n')
validate_path_regex = re.compile(r'[a-z,A-Z]:\\?(\\?\w*\\?)*')
mo = validate_path_regex.search(scan_folder)
if mo is None:
    print('Path is not valid. Please re-enter path.\n')
    import sys
    sys.exit()
os.chdir(scan_folder)
# get find/replaceStrings, and then confirm that inputs are correct.
find_string = input('Enter the text you wish to find:\n')
replace_string = input('Enter the text to replace:\n')
permission = input('\nPlease confirm you want to replace '
                   + find_string + ' with '
                   + replace_string + ' in ' + scan_folder
                   + ' directory.\n\nType "yes" to continue.\n')
if permission == 'yes':
    change_count = 0
    # Context manager for results file
    with open('find_and_replace.txt', 'w') as results:
        for root, subdirs, files in os.walk(scan_folder):
            for file in files:
                # ignore files that don't endwith '.mpr'
                if os.path.join(root, file).endswith('.mpr'):
                    fullpath = os.path.join(root, file)
                    # context manager for each file opened
                    with open(fullpath, 'r+') as f:
                        text = f.read()
                        # only add to changeCount if find_string is in text
                        if find_string in text:
                            change_count += 1
                        # move cursor back to beginning of the file
                        f.seek(0)
                        f.write(text.replace(find_string, replace_string))
        results.write(str(change_count)
        + ' files have been modified to replace '
        + find_string + ' with ' + replace_string + '.\n')
    print('Done with replacement')
else:
    print('Find and replace has not been executed')
end = time.time()
print('Program took ' + str(round((end - start), 4)) + ' secs to complete.\n')

查找字符串=BM=“LS”\nTI=“12”\nDU=“7” 替换\u string=BM=“LSL”\nDU=“7”

原始文件看起来像

BM="LS"
TI="12"
DU="7" 
我想把它改成

BM="LSL"
DU="7" 

但是文件没有改变。

因此,您的误解是源代码(理解转义序列,如
“这是一个\n有两行的字符串”
)与“原始字符串”(在本上下文中没有意义的概念)以及作为用户输入提供的数据之间的区别。
input
功能基本上处理来自标准输入设备的数据。当您向标准输入提供数据时,它将被解释为原始字节,然后
input
函数将其假定为文本(使用您的系统设置进行解码)。有两种方法允许用户输入换行符,第一种是使用
sys.stdin
,但是,这需要您提供EOF,可能需要使用ctrl+D:

这对用户不是很友好。您必须传递换行符和EOF,即返回+ctrl+D或执行ctrl+D两次,我相信这取决于系统

更好的方法是允许用户输入转义序列,然后自己解码:

>>> x = input()
I want this to\nbe on two lines
>>> x
'I want this to\\nbe on two lines'
>>> print(x)
I want this to\nbe on two lines
>>> x.encode('utf8').decode('unicode_escape')
'I want this to\nbe on two lines'
>>> print(x.encode('utf8').decode('unicode_escape'))
I want this to
be on two lines
>>>

您的意思是当您在终端中输入
\n
作为用户输入时?因为这将被字面解释为反斜杠字符和转义
input
基本上是等待用户输入直到换行,然后返回的字符串不包括换行符。
在这里做了大量工作。打开和迭代文件的代码怎么可能不相关?我还将创建一个新文件,然后移动该文件,这将给我更大的灵活性,而不是移动文件指针。@juanpa.arrivillaga不,我正在将文件中的整个字符串键入终端,包括任何“\n”字符。我试过做'\\n',但也没用。@roganjosh我用完整的代码编辑了这篇文章。
>>> x = input()
I want this to\nbe on two lines
>>> x
'I want this to\\nbe on two lines'
>>> print(x)
I want this to\nbe on two lines
>>> x.encode('utf8').decode('unicode_escape')
'I want this to\nbe on two lines'
>>> print(x.encode('utf8').decode('unicode_escape'))
I want this to
be on two lines
>>>