简体   繁体   English

使用 python 将文本文件逐行读入字符串

[英]Read a text file line-by-line into a string with python

I have made a keylogger with python that encrypts and saves keylogs as numbers to a textfile (data.txt).我用 python 制作了一个键盘记录器,它可以加密键盘日志并将其作为数字保存到文本文件(data.txt)中。 I also wanted it to send the encrypted keylogs (numbers) to my gmail account using smtplib.我还希望它使用 smtplib 将加密的键盘日志(数字)发送到我的 gmail 帐户。 the problem is that i get this output:问题是我得到了这个 output:

5
5
6
5
6
2
1
0
4
4
4
7
5
9
5
\
,
4
2
3
8
6
9
1
0
1
5
3
7
2
5
6
\

Instead of this:而不是这个:

123456789108765
682371638321237

I have been using this code to get all text from the textfile as a list:我一直在使用此代码从文本文件中获取所有文本作为列表:

with open("data.txt") as f:
                x=(f.readlines())

And this code to get all items from that list and print them line by line:这段代码从该列表中获取所有项目并逐行打印它们:

x = '\n'.join(map(str, x))
        print(str(x))
        message = str(x) # this message gets sent to my gmail account

If you feel that this question is misssing some information that you may need to answer my question then i would be glad to include that information in my question!如果您觉得这个问题缺少一些您可能需要回答我的问题的信息,那么我很乐意在我的问题中包含这些信息!

Thanks in Advance:)提前致谢:)

you may want to replace:您可能需要更换:

x = '\n'.join(map(str, x))

with:和:

x = ' '.join(map(str, x))   # results in `1 2 3 4 5` etc

or remove the space completely, if you want your numbers like this 123456789或完全删除空格,如果您想要这样的号码123456789

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

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