【python】python删去文件中的回车、空格等

英语渣在看外国文献时喜欢谷歌成段直接翻译,但是pdf文字直接粘贴存在换行符,谷歌翻译不能识别,所以用python删去。
这个故事告诉大家要好好学英语。

参考

链接1中的大哥写的已经完全能够实现功能了,但是可能python版本比较老所以报了好多提示,因此做了一些修改。(python 3.8 PEP8)

源码

def strip_file(old_f_name, new_f_name):
    """remove the space or Tab or enter in a file,and output to a new file in the same folder"""
    fp = open(old_f_name, 'r', encoding='gb18030')
    new_fp = open(new_f_name, "w")
    for each_line in fp.readlines():
        # newStr = each_line.replace(" ", "").replace("\t", "").strip()
        new_str = each_line.replace("\n", " ")
        # print "Write:",newStr
        new_fp.write(new_str)
    fp.close()
    new_fp.close()


if __name__ == "__main__":
    oldName = input("input file name:")
    nameList = oldName.split(".")
    newName = "%s%s%s" % (nameList[0], "_new.", nameList[1])
    strip_file(oldName, newName)
    print("finish output to new file:", newName)

使用

  • 输入文件名(eg. 123.txt)
This book deals with the problem of trajectory planning, i.e. of the
computation of desired motion profiles for the actuation system of automatic
machines. Because of their wide use, only electric drives are
considered here, and their motion is defined in the context of the realtime
control of automatic machines with one or more actuators, such
as packaging machines, machine-tools, assembly machines, industrial
robots, and so on. In general, for the solution of this problem some
specific knowledge about the machine and its actuation system is also
required, such as the kinematic model (direct and inverse) (usually
the desired movement is specified in the operational space, while the
motion is executed in the actuation space and often these domains
are different) and the dynamic model of the system (in order to plan
suitable motion laws that allow to execute the desired movement with
proper loads and efforts on the mechanical structure). Moreover, for
the real-time execution of the planned motion, it is necessary to define
proper position/velocity control algorithms, in order to optimize
the performances of the system and to compensate for disturbances
and errors during the movements, such as saturations of the actuation
system. Several techniques are available for planning the desired
movement, each of them with peculiar characteristics that must be well
known and understood. In this book, the most significant and commonly
adopted techniques for trajectory planning are illustrated and
analyzed in details, taking into account the above mentioned problems.
  • 生成123_new.txt
This book deals with the problem of trajectory planning, i.e. of the computation of desired motion profiles for the actuation system of automatic machines. Because of their wide use, only electric drives are considered here, and their motion is defined in the context of the realtime control of automatic machines with one or more actuators, such as packaging machines, machine-tools, assembly machines, industrial robots, and so on. In general, for the solution of this problem some specific knowledge about the machine and its actuation system is also required, such as the kinematic model (direct and inverse) (usually the desired movement is specified in the operational space, while the motion is executed in the actuation space and often these domains are different) and the dynamic model of the system (in order to plan suitable motion laws that allow to execute the desired movement with proper loads and efforts on the mechanical structure). Moreover, for the real-time execution of the planned motion, it is necessary to define proper position/velocity control algorithms, in order to optimize the performances of the system and to compensate for disturbances and errors during the movements, such as saturations of the actuation system. Several techniques are available for planning the desired movement, each of them with peculiar characteristics that must be well known and understood. In this book, the most significant and commonly adopted techniques for trajectory planning are illustrated and analyzed in details, taking into account the above mentioned problems.

后续

复制粘贴过程还是过于繁琐,需求简化。

其他

猜你喜欢

转载自blog.csdn.net/weixin_44229927/article/details/111584198