简体   繁体   English

来自.txt文件的Python列表

[英]Python list from .txt file

I`m trying to create primitive dictionary (or list) in Python with basic functions that I learned from book (Mark Summerfield). 我正在尝试使用我从书中学到的基本功能(Mark Summerfield)在Python中创建原始字典(或列表)。

Here is my question. 这是我的问题。

All works as I need, but I had a problem with writing strings to the file. 一切都按我需要的方式工作,但是我在将字符串写入文件时遇到了问题。

This is a piece of my code: 这是我的一段代码:

  word = input("New word is: ") #adding new word
  if word:
       word.strip() #erasing new str from any symbols
       x = len(word)
       numeration = str(count) + ". " 
       word = word[:0] + numeration + word[0:] # =to sort as list
       word = word[:x] + ",\n" # =adding shift to the end of str
       file = open("test.txt", "a") # =wriing to the file 
       file.write(word)
       file.close()
       count += 1

Problem shows itself in the output: 问题在输出中显示:

======================== RESTART: /home/z177/test.py ========================
Type word to add word, type nothing to read dict, ^D\Z to quit
New word is: currently raining
New word is: currently not
New word is: 
1. currently rain,
2. currently ,

It is overwriting the last 3 symbols in the string. 它覆盖了字符串中的最后3个符号。

I've solved this problem by replacing 我已经通过更换解决了这个问题

word[:x] + ",\n" 

with

word + ",\n"

I am still interested why the code is replacing these 3 symbols when I add new symbols to the end of the string. 当我在字符串的末尾添加新符号时,我仍然感兴趣为什么代码正在替换这3个符号。

Can you explain this? 你能解释一下吗?

The issue is created in this line: 问题是在这一行中创建的:

word = word[:x] + ",\n" # =adding shift to the end of str

You need to remember that variable word isn't contain your word any more. 你需要记住,变量word不再包含你的单词了。 You have there for example "0. word" there. 你那里有例如“0. word”。

So you have to update x - line count to get correct results in your file. 因此,您必须更新x行计数才能在文件中获得正确的结果。

The length of word has increased after inserting numeration , so x holds the value of the old length before the update. 插入numeration后,单词的长度增加,因此x在更新之前保存旧长度的值。 You should move the line where x is assigned after the update: 您应该在更新后移动指定x的行:

numeration = str(count) + ". " 
word = numeration + word[0:] # length of word changes here
x = len(word)
word = word[:x] + ",\n"

Or stick to using word = word + ",\\n" which is less verbose and still does the same thing. 或坚持使用word = word + ",\\n" ,这不是那么冗长,仍然做同样的事情。


The entire logic can however be simplified into the following: 但是,整个逻辑可以简化为以下内容:

word = numeration + word + ',\n'  

Your problem is that you set x to be the length of the input word, but then add numeration to the string, making it several characters longer (three characters, in the case of a single-digit count ). 您的问题是您将x设置为输入字的长度,但随后将字符numeration添加到字符串中,使其更长几个字符(在单个数字count的情况下为三个字符)。 If you were to change 如果你要改变

word = word[:x] + ",\n" # =adding shift to the end of str

to

word = word[:len(word)] + ",\n"

You should get the correct result. 你应该得到正确的结果。

Unrelated to your issue, the code you presented is very un-Pythonic, and when you've mastered the logic of the program I'd take a look at refactoring, using with open('test.txt', 'a') as file: over the explicit file.close() and using str.format() over concatenation. 与您的问题无关,您提供的代码非常不具有Pythonic,当您掌握了程序的逻辑时,我会看一下重构,使用with open('test.txt', 'a') as file:通过显式with open('test.txt', 'a') as file: file.close()并使用str.format()进行连接。

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

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