python中的input()和\ n字符

时间:2019-06-21 18:33:49

标签: python python-3.x

我正在尝试使用input()在多个文件中查找并替换多行纯文本,但是当我输入'\ n'字符来表示新换行符在文本中的位置时,它找不到并不会取代它。

我尝试使用raw_strings,但无法使它们工作。

这是正则表达式的工作吗?

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')

find_string = BM =“ LS” \ nTI =“ 12” \ nDU =“ 7” replace_string = BM =“ LSL” \ nDU =“ 7”

原始文件看起来像

BM="LS"
TI="12"
DU="7" 

,我希望将其更改为

BM="LSL"
DU="7" 

但是文件没有改变。

1 个答案:

答案 0 :(得分:2)

因此,您的误解是区分源代码和诸如“原始字符串”(在这种情况下没有意义的概念)以及您作为用户输入提供的数据。 "this is a string \n with two lines"函数基本上处理从标准输入设备传入的数据。当您向标准输入提供数据时,它会被解释为原始字节,然后input函数会假定其含义是文本(使用系统设置所暗示的内容进行解码)。允许用户输入换行符的方法有两种,第一种是使用input,但是,这将要求您提供EOF,可能使用 ctrl + D < / kbd>:

sys.stdin

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

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

>>> import sys
>>> x = sys.stdin.read()
here is some text and i'm pressing return
to make a new line. now to stop input, press control d>>> x
"here is some text and i'm pressing return\nto make a new line. now to stop input, press control d"
>>> print(x)
here is some text and i'm pressing return
to make a new line. now to stop input, press control d