简体   繁体   English

从带有空格的列表中创建文件

[英]Creating a file from a list with spaces

from collections import OrderedDict
sentence= ("I met a traveller from an antique land,Who said—“Two vast and trunkless legs of stoneStand in the desert. . . . Near them, on the sand,Half sunk a shattered visage lies, whose frown,And wrinkled lip, and sneer of cold command,Tell that its sculptor well those passions readWhich yet survive, stamped on these lifeless things,The hand that mocked them, and the heart that fed;And on the pedestal, these words appear:My name is Ozymandias, King of Kings;Look on my Works, ye Mighty, and despair!Nothing beside remains. Round the decayOf that colossal Wreck, boundless and bareThe lone and level sands stretch far away.").lower()

words = sentence.split(' ') 单词=句子.split('')

lst = list(OrderedDict.fromkeys(words))
numberLst = []
for i in words:
   numberLst.append(lst.index(i)+1)

print(lst)
print (numberLst)

words_str = ''.join(words)
numberLst_str = ''.join(str(e) for e in numberLst)

file = open("words.txt","w")
file.write(words_str)
file.close()

file=open("numberlst.txt","w")
file.write(numberLst_str)
file.close()

joinlst = " ".join(lst[i-1] for i in numberLst)

print (joinlst)

file=open("joinlst.txt","w")
file.write(joinlst)
file.close()

Hello, i have a problem with my words file that i have created in my code and it is that when i connect all of the words in my list it is written into the file like this: imetatravellerfromanantiqueland,whosaid—“twovastandtrunklesslegsofstonestandinthedesert....nearthem,onthesand,halfsunkashatteredvisagelies,whosefrown,andwrinkledlip,andsneerofcoldcommand,tellthatitssculptorwellthosepassionsreadwhichyetsurvive,stampedontheselifelessthings,thehandthatmockedthem,andtheheartthatfed;andonthepedestal,thesewordsappear:mynameisozymandias,kingofkings;lookonmyworks,yemighty,anddespair!nothingbesideremains.roundthedecayofthatcolossalwreck,boundlessandbaretheloneandlevelsandsstretchfaraway. 您好,我在我的代码中创建的word文件有问题,就是当我连接列表中的所有单词时,它会被写入文件,如下所示:imetatravellerfromanantiqueland,有人说“ twovastandtrunklesslegsofstonestandintheert”。 nearthem,onthesand,halfsunkashatteredvisagelies,whosefrown,andwrinkledlip,andsneerofcoldcommand,tellthatitssculptorwellthosepassionsreadwhichyetsurvive,stampedontheselifelessthings,thehandthatmockedthem,andtheheartthatfed; andonthepedestal,thesewordsappear:mynameisozymandias,kingofkings; lookonmyworks,yemighty,anddespair nothingbesideremains.roundthedecayofthatcolossalwreck,boundlessandbaretheloneandlevelsandsstretchfaraway。

The problem is that there are no spaces between the words written in the file so it is hard to identify the words in the file. 问题在于文件中写入的单词之间没有空格,因此很难识别文件中的单词。 once fixed it would preferably look like this : (i:met:a:traveller:from:an:antique:land)(just as an example) 一旦修复,它最好看起来像这样:(i:met:a:traveller:from:an:antique:land)(仅作为示例)

if you could please help it would be greatly appreciated, thank you. 如果您能帮助的话,将不胜感激,谢谢。

The problem is that, when you join the words at the beginning you use ''. 问题是,当您在开头加入单词时,您会使用”。 You should change that line from: 您应该从以下位置更改该行:

words_str = ''.join(words)

to: 至:

words_str = ' '.join(words)  # Separates the words with spaces

If you want it separated by regular spaces, or 如果您希望将其用规则的空格隔开,或者

words_str = ':'.join(words)  # Separates the words with ':'

If you want it separated by ':' 如果要用“:”分隔

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

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