简体   繁体   English

Python:1)如何引用两个字符串变量并在其间插入一个列表元素? 2) 我怎样才能将 append 的字符串构造成一个列表?

[英]Python: 1) How to reference two string variables and insert a list element in between? 2) How can I append the string constructed to a list?

Here is a.txt file (semi colon signifies start of file):这是一个 .txt 文件(分号表示文件的开始):

MiddlePhrase.txt:
coke and cheeseburgers
bread and wine
beer and tacos

Here is the code I have now:这是我现在拥有的代码:

middlePhrases = open('MiddlePhrase.txt', 'r')

firstPhrase = 'I love to drink and eat '
secondPhrase = ' when I am at the beach!'

for i in middlePhrases:
    print(beginningPhrase,{},endPhrase)

The goal is to write the first line of 'MiddlePhrase.txt' to a new.txt file named 'NewPhraseFile.txt', then append the following two lines in 'MiddlePhrase.txt' to the 'NewPhraseFile.txt'目标是将'MiddlePhrase.txt'的第一行写入一个名为'NewPhraseFile.txt'的new.txt文件,然后将'MiddlePhrase.txt'中的append以下两行写入'NewPhraseFile.txt'

This is the desired output (a new.txt file being written with content):这是所需的 output(正在写入内容的 new.txt 文件):

NewPhraseFile.txt:
I love to drink and eat coke and cheeseburgers when I am at the beach!
I love to drink and eat bread and wine when I am at the beach!
I love to drink and eat beer and tacos when I am at the beach!

I hope this works for you...我希望这对你有用......

firstPhrase = 'I love to drink and eat '
secondPhrase = ' when I am at the beach!'

fp = open('MiddlePhrase.txt','r')
#reads each line
for i in fp:
    # getting string of desired format.
    text = firstPhrase+i.strip()+secondPhrase+'\n'
    #storing it in NewPhraseFile.txt
    with open("NewPhraseFile.txt",'a') as new:
        new.write(text)
    new.close()

Output in NewPhraseFile.txt: NewPhraseFile.txt 中的 Output:

I love to drink and eat coke and cheeseburger when I am at the beach!
I love to drink and eat bread and wine when I am at the beach!
I love to drink and eat beer and tacos when I am at the beach!

A short version一个简短的版本

open('new.txt', 'a+').writelines(
    f'I love to drink and eat {x.strip()} when I am at the beach!\n'
    for x in open('mid.txt'))

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

相关问题 我怎么能 append 一个字符串到列表中的元组? - How I can append a string to the tuples in a list? 如何在python中获取列表的列表的String的第n个元素? - How can I get the nth element of String for list of list in python? 如何在python的列表元素中拆分两个字符? - How can I split between two characters in a list element in python? Python附加到列表中的字符串元素 - Python append to an string element in a list 如何 append 和 integer 到 Python 中嵌套列表中的字符串元素 - How to append an integer to a string element in a nested list in Python 在Python中动态地将值/字符串追加/插入列表元素? - append/insert a value/string to a list element dynamically in Python? 如何在python中引用列表中的字符串 - How to reference a string in a list in python 我怎样才能将 append 多个字符串变量放到一个列表中,然后再分开? - How can I append multiple string variables into a single list, and have then separate? Python正则表达式-将两个单词之间的文本捕获为字符串,然后追加到列表中 - Python regex - capture text between two words as string, then append to list 如何将具有两个变量的列表追加到已经具有该列表但具有不同值的另一个列表? 蟒蛇 - how do I append a list with two variables to another list that already has that list in it but with different values? python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM