繁体   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