简体   繁体   English

是否可以从1个文件中读取旋转文本并将其写入另一个文本文件中,而在python3中没有括号?

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

I am stuck & i need your help guys. 我被困住了,我需要你们的帮助。 Here is what i want this program to do. 这是我想要该程序执行的操作。 I have 1 text file named spin.txt which contains spin text like {something there|Source|something here} baz {why not find out more|these guys} & i have another text file named write.txt now i want to read from spin.txt file & write it to write.txt file. 我有1个名为spin.txt的文本文件,其中包含旋转文本,例如{something | Source | something} baz {为什么找不到更多信息? spin.txt文件并将其写入write.txt文件。 What i have done. 我做了什么。 Everything is working fine but the only problem i am getting is when writing to write.txt file it adds bracket & ' like this - [' wrote here'] but i don't want this i only want to write that text. 一切工作正常,但我遇到的唯一问题是在写入write.txt文件时,它添加了括号&'像这样-['写在这里'],但我不想这样做,我只想编写该文本。 I am using python 3x on windows 10. 我在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] ” .join(spec)[2:-2]

Just changing this in code it works. 只需在代码中更改它即可。 Thanks for reply @bit 感谢您的回复@bit

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM