简体   繁体   English

在 python 中将文本数据从一个 txt 文件复制到另一个文件时出现问题

[英]Having problem while copying text data from one txt file to another in python

I have a file text.txt that contains text data: this is a text file我有一个包含文本数据的文件 text.txt:这是一个文本文件
then I have a test.text that is primarily empty.然后我有一个 test.text 主要是空的。

from sys import argv

script, from_file, to_file = argv

out_file = open(to_file, 'w').write((open(from_file).read()))

when I run that code the test.txt file gets copied text from text.txt file and it is:当我运行该代码时, test.txt 文件会从 text.txt 文件中复制文本,它是:

this is a text file਍ഀ
਍ഀ

why do I get those rectangular blocks??为什么我得到那些矩形块? How to fix it??怎么解决??

If you look closely at the "rectangular blocks" you can see that they contain the rows如果您仔细查看“矩形块”,您会发现它们包含行

0A

and

0D

which are the hex representations of '\n' and '\r' respectively.它们分别是'\n''\r'的十六进制表示。 So the blocks are your editor's way of showing you these characters, which are used in Windows to mark the end of a line in a text file and which are usually invisible.因此,这些块是您的编辑器向您展示这些字符的方式,这些字符在 Windows 中用于标记文本文件中的行尾,这些字符通常是不可见的。

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

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