使用 python 解析文件中的数据

发布于 2025-01-14 04:11:17 字数 517 浏览 4 评论 0原文

我有两个文件,一个是模板文件,另一个是包含模板文件值的文件。我试图获取模板文件,然后将值从另一个文件传递给变量,并将这两个文件合并到第三个文件中。我可以使用以下代码片段将一个文件复制到另一个文件 `

print("Enter the Name of Source File: ")
sFile = input()
print("Enter the Name of Target File: ")
tFile = input()
fileHandle = open(sFile, "r")
texts = fileHandle.readlines()
fileHandle.close()

fileHandle = open(tFile, "w")
for s in texts:
    fileHandle.write(s)
fileHandle.close()

print("\nFile Copied Successfully!")

`

但是我不知道如何对两个或多个文件执行此操作,然后将它们制作成一个文件。任何帮助/指导表示赞赏

I have two files one template file and one file which has the values for the template file. I am trying to take the template file and then pass values to the variables from another file and combine the two into a third file. I am able to copy one file to another using the following snippet of code
`

print("Enter the Name of Source File: ")
sFile = input()
print("Enter the Name of Target File: ")
tFile = input()
fileHandle = open(sFile, "r")
texts = fileHandle.readlines()
fileHandle.close()

fileHandle = open(tFile, "w")
for s in texts:
    fileHandle.write(s)
fileHandle.close()

print("\nFile Copied Successfully!")

`

however I am not sure how to do it for two or more files and then to make them into one file. Any help/guidance is appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

裂开嘴轻声笑有多痛 2025-01-21 04:11:17

这当然不是最优雅的解决方案,但我认为它应该适合您。

# You could add as many files to this list as you want.
list_of_files = []

count = 1
while True:

    print(f"Enter the Name of Source File{count} (Enter blank when done adding files): ")
    sFile = input()
    # If the input is not empty then add the filename to list_of_files.
    if sFile:
        list_of_files.append(sFile)
        count += 1
    else:
        break

print("Enter the Name of Target File: ")
tFile = input()

# With open will open the file and then close if when done.
with open(tFile, 'a+') as target:
    # This will loop over all the files in your list. 
    for file in list_of_files:
        tmp = open(file, 'r')
        target.write('\n' + tmp.read())
        tmp.close()

This is certainly not the most elegant solution but I think it should work for you.

# You could add as many files to this list as you want.
list_of_files = []

count = 1
while True:

    print(f"Enter the Name of Source File{count} (Enter blank when done adding files): ")
    sFile = input()
    # If the input is not empty then add the filename to list_of_files.
    if sFile:
        list_of_files.append(sFile)
        count += 1
    else:
        break

print("Enter the Name of Target File: ")
tFile = input()

# With open will open the file and then close if when done.
with open(tFile, 'a+') as target:
    # This will loop over all the files in your list. 
    for file in list_of_files:
        tmp = open(file, 'r')
        target.write('\n' + tmp.read())
        tmp.close()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文