如何创建文本文件,命名和用内容填充?

时间:2018-10-16 16:03:02

标签: 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í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在我的计算机上可用。

我该怎么做?

3 个答案:

答案 0 :(得分:1)

您可以浏览每行并将其分成三个空格:

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]
}

答案 1 :(得分: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)

逐行迭代定义文件的内容,将每一行拆分为文件名和定义部分,然后将定义写入具有适当名称的文件中。

答案 2 :(得分:0)

阅读

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

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