Text 如何创建文本文件、命名并用内容填充?

Text 如何创建文本文件、命名并用内容填充?,text,Text,我有一个文本文件,里面有一个单词列表和定义,就像这样 的 of / ~'s (possessive particle) / (used after an attribute) / (used to form a nominal expression) / (used at the end of a declarative sentence for emphasis) 我 I / me / my 你 you (informal, as opposed to courteous 您[ní

我有一个文本文件,里面有一个单词列表和定义,就像这样

的   of / ~'s (possessive particle) / (used after an attribute) / (used to form a nominal expression) / (used at the end of a declarative sentence for emphasis)
我   I / me / my
你   you (informal, as opposed to courteous 您[nín])
是   is / are / am / yes / to be / variant of 是[shì] / (used in given names)
了   (modal particle intensifying preceding clause) / (completed action marker)
不   (negative prefix) / not / no
我需要在Windows 10中为列表中的每个中文单词创建一个文本文件,文件内容为英文定义

例如,我需要一个名为的.txt

…文件中包含此内容:

of / ~'s (possessive particle) / (used after an attribute) / (used to form a nominal expression) / (used at the end of a declarative sentence for emphasis)
我相当精通Javascript,熟悉python,并且在我的计算机上有powershell


我如何才能做到这一点?

您只需查看每一行,并将其拆分为三个空格:

动力壳

$Content = Get-Content -Path C:\Temp\Temp.txt
foreach ( $Line in $Content )
{
    $Temp = $Line -split '   ' | Where-Object -FilterScript { -not [System.String]::IsNullOrWhiteSpace($_) }
    New-Item -Path "C:\Temp" -Name "$($Temp[0]).txt" -Value $Temp[1]
}

如果我理解正确的话,这应该可以解决问题

with open('yourfile.txt') as definitions:
    for line in definitions:
        name, definition = line.split(maxsplit=1)
        with open(name + '.txt', 'w') as out:
            out.write(definition)
它逐行迭代定义文件的内容,将每行拆分为文件名和定义部分,然后将定义写入具有适当名称的文件中。

读取

with open ("File_name","r") as f:
    f.read()

with open ('file_name','w') as f:
    f.write('content')

只要一些约束条件成立,这就相当简单了。你知道如何迭代文件行、拆分字符串和创建文件吗?另外,请决定解决方案应该使用哪种编程语言,并删除其他标记。这个答案应该提到它是用Python编写的。这是Python吗?@webmagnets Yes我试过你的代码,得到了以下…>>>以打开的“C:\Users\Nathan Cain\Downloads\sample.txt”作为定义:。。。对于定义中的行:。。。名称,定义=line.splitmaxsplit=1。。。使用openname+'.txt',w'作为输出:。。。out.writedefinition。。。文件,第1行语法错误:unicode错误“UnicodeScape”编解码器无法解码位置2-3处的字节:截断\UXXXXXXXX escape>>>是否需要执行一些特殊操作才能读取中文?@webmagnetics确实有帮助?这有点帮助。现在我得到了。。。回溯最近一次调用:文件C:\Users\Nathan Cain\AppData\Local\Programs\Python\Python37\lib\encodings\cp1252.py,第23行,在decode return codecs.charmap\u decodeinput,self.errors,decoding\u table[0]UnicodeDecodeError中:“charmap”编解码器无法解码405位置的字节0x8d:字符映射到我尝试过这个,但我犯了一个错误,可能是因为它是中国人。新项目:找不到路径“C:\Users\Nathan Cain\Downloads\sample\皨txt”的一部分。第5行字符:5+新项目-路径C:\Users\Nathan Cain\Downloads\sample \-Name$…+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~CategoryInfo:WriteError:C:\Users\Nathan…\sample\çš.txt:String[New Item],DirectoryNotFoundException+FullyQualifiedErrorId:NewItemIOError,Microsoft.PowerShell.Commands.NewItemCommands更新了我的脚本,因为我从未运行过它。它应该有三重空间。。。哎呀。还添加了对空白的处理。@webmagnetics我无法复制您的问题。我在Windows 10上使用PS 5.1,创建文件时没有问题。这有点效果,但我的文件名不稳定。。。模式LastWriteTime长度名称-----a--2018年10月16日上午11:46 155ššš.txt-a--2018年10月16日上午11:46上午11æˆ.txt-a--2018年10月16日上午11:46 55ä½。txt@webmagnets必须使用unicode字符。尝试用$Temp[1]| Out File-FilePath C:\Temp\$$Temp[0].txt-Encoding unicode-Force替换新项-C:\Temp-Name$$Temp[0].txt-Value$Temp[1]。这只是写文件的另一种方式。