简体   繁体   English

如何复制文本文件内容并将其转换为行

[英]How to copy a text file content and convert it into lines

I am trying to make a program that is able to copy a text file and then write out another text file with the same content but changed.我正在尝试制作一个能够复制文本文件的程序,然后写出另一个具有相同内容但已更改的文本文件。 For example:例如:

SAS- numbers
PR  - 123
SE  - 456
TE  - 789

The one above is the original text file, but I would like to know how can I make it so it looks like this when my program writes out the copy of the original one:上面的一个是原始文本文件,但我想知道如何制作它,所以当我的程序写出原始文件的副本时它看起来像这样:

numbers;123;456;789

This is what I have for now:这就是我现在所拥有的:

    openfile = input('Enter the input file: ')
    outputfile = input('Enter the output file: ')

    output = open(outputfile,'w')
    with open(openfile, 'r') as inputfile:
                output.write(inputfile.read())
    output.close()

Any advice would be really helpful!任何建议都会非常有帮助!

Look into my Below code.查看我的以下代码。 This might help you.这可能会帮助你。

input_data = ""
with open("input.txt", "r") as f:
    input_data = f.readlines()

output_data = ";".join([line.split("-")[1].rstrip("\n").strip() for line in input_data])

with open("output.txt", "w") as f:
    f.write(output_data)

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

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