簡體   English   中英

是否可以從1個文件中讀取旋轉文本並將其寫入另一個文本文件中,而在python3中沒有括號?

[英]Is it possible to read spin text from 1 file & write it in another text file without bracket in python3?

我被困住了,我需要你們的幫助。 這是我想要該程序執行的操作。 我有1個名為spin.txt的文本文件,其中包含旋轉文本,例如{something | Source | something} baz {為什么找不到更多信息? spin.txt文件並將其寫入write.txt文件。 我做了什么。 一切工作正常,但我遇到的唯一問題是在寫入write.txt文件時,它添加了括號&'像這樣-['寫在這里'],但我不想這樣做,我只想編寫該文本。 我在Windows 10上使用python 3x。

**My code :-**

import re
import itertools

p = re.compile("(\{[^\}]+\}|[^\{\}]*)")

spin_comments  =open("C:\\Users\\Desktop\\spin.txt","r")
file = str(spin_comments.readlines())

frags = p.split(file)

def options(s):
    if len(s) > 0 and s[0] == '{':
        return [opt for opt in s[1:-1].split('|')]
    return [s]

opt_lists = [options(frag) for frag in frags]

write_text = open("C:\\Users\\Desktop\\write.txt","w")


for spec in itertools.product(*opt_lists):
    a = ''.join(spec)
    write_text.write(a + '\n')
    print(a)
write_comment.close()

” .join(spec)[2:-2]

只需在代碼中更改它即可。 感謝您的回復@bit

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM