简体   繁体   English

python:从.txt读取并判断txt文件中的新字符串

[英]python: read from .txt and judge the new string in the txt file

I am trying to read/write txt file. 我正在尝试读取/写入txt文件。 I want to read from txt file, and then write new string to the next line if the string not existed in the original file. 我想从txt文件中读取内容,然后如果原始文件中不存在该字符串,则将新字符串写入下一行。

f = open('./video/1.txt', 'a')
f.write('123' + '\n')
f.close()

with open('./video/1.txt') as o:
    lines=o.readlines()

lines=set(lines)

if '123' in lines:
    print("Existed")
else:print("Not Existed")

The question is : i have '123' is the 1.txt file. 问题是:我有“ 123”是1.txt文件。 And the result is "Not Existed" And I tried to use 结果是“不存在”,我尝试使用

for line in lines:
    print(line + '\n')

to print out the set variable "lines", it print out 123 at one line. 要打印出设置变量“ lines”,它在一行中打印出123。 I am confused now. 我现在很困惑。 What causes the "NOT EXISTED"? 是什么原因导致“不存在”? Thank you. 谢谢。

Update: I uploaded the screenshot. 更新:我上传了屏幕截图。 Still confused. 还是很困惑。 在此处输入图片说明

Your if statement is checking if an element of the set lines is ' 123 ' 您的if语句正在检查set lines的元素是否为“ 123

example: 例:

lines = ['hello there', 'hello 123 there', '123']
if '123' in lines:
    print("Existed")
else:print("Not Existed")

output: 输出:

Existed

example: 例:

lines = ['hello there', 'hello 123 there']
if '123' in lines:
    print("Existed")
else:print("Not Existed")

output: 输出:

Not Existed

You could do something like this: 您可以执行以下操作:

lines = ['hello there', 'hello 123 there']
for line in lines:
    if '123' in line:
        print('Existed')

If you are reading from file, you could be getting a trailing carriage-return character in you line, this is \\n . 如果您正在从文件中读取,则可能会在行中出现尾随回车符,这是\\n This could also cause your if '123' in lines to go wrong, even when the line is only ' 123 ', reality is the line is actually ' 123\\n '. 这也可能导致您的if '123' in lines出错,即使该行仅是“ 123 ”,实际情况是该行实际上是“ 123\\n ”。

example: 例:

ms = '123\n'
print(ms)
mb = ms.encode()
print(mb)

output: 输出:

123

b'123\n'

in this sample output, as well as in your screenshot, you can actually see the extra carriage return in the output. 在此示例输出以及屏幕截图中,您实际上可以在输出中看到额外的回车符。 Hence your string is not ' 123 ' your string is ' 123\\n ' 因此,您的字符串不是' 123 '您的字符串是' 123\\n '

Thanks for @Edwin van Mierlo 's answer, i got this now. 感谢@Edwin van Mierlo的回答,我现在明白了。 This is a '\\n' after 123. So if i use '123\\n' instead of 123, it shows existed. 这是123之后的“ \\ n”。因此,如果我使用“ 123 \\ n”而不是123,则表明存在。

if '123\n' in lines:
    print("Existed")
else:print("Not Existed")

Late answer but anyway 答案较晚,但无论如何

Instead of keep using lines as a variable I will use lines1 与其继续使用lines作为变量,不如使用lines1

In [3]: with open('1.txt') as o:
   ...:     lines = o.readlines()
   ...:

In [4]: lines1 = set(lines)

If you do a print on lines1 , this is the result you have. 如果您在lines1上进行打印,这就是您得到的结果。

In [5]: print(lines1)
{'123\n'}

Where does this take you? 这带你去哪里?

One possible solution is to use a regex to look for 123 in your set, if you wish to continue with your set. 一种可能的解决方案是,如果希望继续使用集合,则使用正则表达式在集合中查找123

The regex \\d+ will look for digit. 正则表达式\\d+将查找数字。

In [6]: import re

In [7]: if re.search(r'\d+', str(lines1)):
   ...:     print('yes')
   ...: else:
   ...:     print('no')
   ...:
yes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM