Python 列表和字符串格式设置困难:导入txt文件、添加字符串并将其写入新文件

Python 列表和字符串格式设置困难:导入txt文件、添加字符串并将其写入新文件,python,string,list,formatting,file-writing,Python,String,List,Formatting,File Writing,我在列表和字符串格式以及将更改写入新文件方面遇到问题。我要找的是: 前串 导入的txt文件包含字符串值列表 串接 其中前面和后面的字符串都已定义,所有内容都将写入新文件 我的最终目标是,当我导入一个包含列表的txt文件并运行代码时,它将被打印到一个新文件中,并在导入的txt文件的列表之前和之后添加预定义的字符串 我现在的代码如下: text_file = open(r"text_file path", "r") lines = text_file.read().split(',') lines.

我在列表和字符串格式以及将更改写入新文件方面遇到问题。我要找的是:

前串 导入的txt文件包含字符串值列表 串接 其中前面和后面的字符串都已定义,所有内容都将写入新文件

我的最终目标是,当我导入一个包含列表的txt文件并运行代码时,它将被打印到一个新文件中,并在导入的txt文件的列表之前和之后添加预定义的字符串

我现在的代码如下:

text_file = open(r"text_file path", "r")
lines = text_file.read().split(',')
lines.insert(0, "String Values Before")
lines.insert("String Values After")
text_file.close()
lines.write("new_file.txt", "w+")
现在的问题是我正在插入列表,而我希望字符串与列表分开

我已经能够使用下面的代码在控制台中生成我希望编写的文件的样子:

FIRMNAME = "apple"
FILETYPE = "apple"
REPLYFILENAME = "apple"
SECMASTER = "apple"
PROGRAMNAME = "apple"

text_file = open(r"textfile path", "r+")
lines = text_file.readlines().split('\n')

print(("START-OF-FILE \nFIRMNAME= ") + FIRMNAME) 

print(("FILETYPE= ") + FILETYPE) 

print(("REPLYFILENAME= ") + REPLYFILENAME) 

print(("SECMASTER= ") + SECMASTER) 

print(("PROGRAMNAME= ") + PROGRAMNAME) 


print("START-OF-FIELDS")

print("END-OF-FIELDS")

print("START-OF-DATA")
pprint.pprint(lines) 
print("END-OF-DATA")
print("END-OF-FILE")

我就是不知道如何把它写入一个新文件!救命啊

你可以这样解决:

newFile = 'your_new_file.txt'
oldFile = 'your_old_file.txt'

# Open the new text file
with open(newFile, 'w') as new_file:
    # Open the old text file
    with open(oldFile, 'r') as old_file:
        # Write the line before the old content
        new_file.write('Line before old content\n')

        # Write old content
        for line in old_file.readlines():
            new_file.write(line)

        # Write line after old content
        new_file.write('Line after old content')

你可以这样解决:

newFile = 'your_new_file.txt'
oldFile = 'your_old_file.txt'

# Open the new text file
with open(newFile, 'w') as new_file:
    # Open the old text file
    with open(oldFile, 'r') as old_file:
        # Write the line before the old content
        new_file.write('Line before old content\n')

        # Write old content
        for line in old_file.readlines():
            new_file.write(line)

        # Write line after old content
        new_file.write('Line after old content')
变量行的类型为list,它没有write方法。 此外,insert还需要一个位置,您的第二次通话缺少该位置

您需要读取文件,相应地使用前缀和后缀值对其进行压缩,然后将其写入相应的输出文件:

with open("text_file_path", "r") as input_file:
    text = input_file.read()

text = '\n'.join(("String Values Before", text, "String Values After"))

with open("new_file.txt", "w+") as output_file:
    output_file.write(text)
变量行的类型为list,它没有write方法。 此外,insert还需要一个位置,您的第二次通话缺少该位置

您需要读取文件,相应地使用前缀和后缀值对其进行压缩,然后将其写入相应的输出文件:

with open("text_file_path", "r") as input_file:
    text = input_file.read()

text = '\n'.join(("String Values Before", text, "String Values After"))

with open("new_file.txt", "w+") as output_file:
    output_file.write(text)
使用pformat。

使用pformat。


最初调用insert方法时出现错误,因为必须提供索引;但是,您可以添加、加入结果列表并写入文件:

text_file = open(r"text_file path", "r")
lines = text_file.read().split(',')
lines.insert(0, "String Values Before")
lines.append("String Values After")
text_file.close()
new_file = open('text_file_path.txt', 'w')
new_file.write(','.join(lines)+'\n')
new_file.close()

最初调用insert方法时出现错误,因为必须提供索引;但是,您可以添加、加入结果列表并写入文件:

text_file = open(r"text_file path", "r")
lines = text_file.read().split(',')
lines.insert(0, "String Values Before")
lines.append("String Values After")
text_file.close()
new_file = open('text_file_path.txt', 'w')
new_file.write(','.join(lines)+'\n')
new_file.close()

使用附加模式和换行符这是什么意思?你能给我看看吗?谢谢lines.writenew_file.txt,a然后可以像这样附加到文件中:file.appendFILETYPE=+FILETYPE+\n使用附加模式和换行符这是什么意思?你能给我看看吗?谢谢lines.writenew_file.txt,a然后可以像这样附加到文件中:file.appendFILETYPE=+FILETYPE+\n