-1

I have a text file with a list of words with definitions, like this...

的   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

I need to create a text file in Windows 10 for each Chinese word in the list with the content of the file being the English definition.

For example I need a text file named 的.txt ...

...with this content in the file:

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'm fairly proficient with Javascript, familiar with python, and have powershell available on my computer.

How can I accomplish this?

webmagnets
  • 2,266
  • 3
  • 33
  • 60
  • 1
    This is pretty straight forward (as long as some constraints hold true). Do you know how to iterate over the lines of a file, split strings, and create files? – timgeb Oct 16 '18 at 16:04
  • 1
    Also, please decide on one programming language the solution should be in and remove the other tags. – timgeb Oct 16 '18 at 16:09

3 Answers3

1

You can just look through each line and split it at triple spaces:

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]
}
Shawn Esterman
  • 2,292
  • 1
  • 10
  • 15
  • I tried this, but I got an error probably having to do with the fact that it is Chinese. New-Item : Could not find a part of the path 'C:\Users\Nathan Cain\Downloads\sample\çš„.txt'. At line:5 char:5 + New-Item -Path "C:\Users\Nathan Cain\Downloads\sample\" -Name "$( ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : WriteError: (C:\Users\Nathan...\sample\çš„.txt:String) [New-Item], DirectoryNotFoundExce ption + FullyQualifiedErrorId : NewItemIOError,Microsoft.PowerShell.Commands.NewItemCommand – webmagnets Oct 16 '18 at 16:36
  • Updated my script because I never ran it. It was supposed to have triple spaces... whoops. Also added handling for whitespace. – Shawn Esterman Oct 16 '18 at 16:44
  • @webmagnets I cannot replicate your issue. I am using PS 5.1 on Windows 10 and don't have that problem creating the files. – Shawn Esterman Oct 16 '18 at 16:45
  • That worked somewhat, but I'm getting wonky file names... Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 10/16/2018 11:46 AM 155 çš„.txt -a---- 10/16/2018 11:46 AM 11 我.txt -a---- 10/16/2018 11:46 AM 55 ä½ .txt – webmagnets Oct 16 '18 at 16:47
  • @webmagnets must be something with unicode characters. Try replacing `New-Item -Path "C:\Temp" -Name "$($Temp[0]).txt" -Value $Temp[1]` with `$Temp[1] | Out-File -FilePath "C:\Temp\$($Temp[0]).txt" -Encoding unicode -Force`. It's just a different way to write a file. – Shawn Esterman Oct 16 '18 at 16:50
  • I got some files, but the filenames aren't encoded properly. Here is an example: 什么.txt – webmagnets Oct 16 '18 at 17:06
  • @webmagnets Yeah. I understand. However, I am unsure why you are not able to save a file using a Chinese character in the name. It is supported by unicode and filenames support unicode characters. – Shawn Esterman Oct 16 '18 at 17:10
1

If I understand correctly, this should do the trick.

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)

It iterates over the content of the definitions-file line by line, splits each line into the filename and definition part, and then writes the definition into a file with the appropriate name.

timgeb
  • 76,762
  • 20
  • 123
  • 145
  • Is this python? – webmagnets Oct 16 '18 at 16:28
  • @webmagnets yes – timgeb Oct 16 '18 at 16:31
  • I tried your code and got the following... >>> with open('C:\Users\Nathan Cain\Downloads\sample.txt') as definitions: ... for line in definitions: ... name, definition = line.split(maxsplit=1) ... with open(name + '.txt', 'w') as out: ... out.write(definition) ... File "", line 1 SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape >>> Is there something special that needs to be done to read the Chinese? – webmagnets Oct 16 '18 at 16:44
  • @webmagnets does [this](https://stackoverflow.com/questions/37400974/unicode-error-unicodeescape-codec-cant-decode-bytes-in-position-2-3-trunca) help? – timgeb Oct 16 '18 at 16:45
  • It helped somewhat. Now I got... Traceback (most recent call last): File "", line 2, in File "C:\Users\Nathan Cain\AppData\Local\Programs\Python\Python37\lib\encodings\cp1252.py", line 23, in decode return codecs.charmap_decode(input,self.errors,decoding_table)[0] UnicodeDecodeError: 'charmap' codec can't decode byte 0x8d in position 405: character maps to – webmagnets Oct 16 '18 at 17:01
  • @webmagnets does [this](https://stackoverflow.com/questions/9233027/unicodedecodeerror-charmap-codec-cant-decode-byte-x-in-position-y-character) help? – timgeb Oct 16 '18 at 17:06
0

to read

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

to write

with open ('file_name','w') as f:
    f.write('content')
petezurich
  • 9,280
  • 9
  • 43
  • 57
Mohit Jain
  • 25
  • 1
  • 6