奇怪的 write()/文件行为

huangapple go评论110阅读模式
英文:

Strange write()/File behaviour

问题

  1. lines = ['错误标题:用户 ' + str(user_id) + ' ' + firstName + ' ' + lastName + ' 数据框为空']
  2. with open(ERROR_LOG_PATH + ERROR_LOG_NAME, 'a') as file:
  3. file.write('\n'.join(lines))
  4. file.write('\n')
  5. file.close()
  1. def 写入日志(lines):
  2. with open(ERROR_LOG_PATH + ERROR_LOG_NAME, 'a') as file:
  3. file.write('\n'.join(lines))
  4. file.write('\n')
  5. file.close()
  1. 写入日志(['错误标题:用户 ' + str(user_id) + ' ' + firstName + ' ' + lastName + ' 数据框为空'])
英文:

Hi I have a long python application, and within it's workflow I am trying to write snippets of strings to an error log.

I was using:

  1. lines = ['ERROR TITLE: user ' + str(user_id) + ' ' + firstName + ' ' + lastName + ' dataframe is empty for this user']
  2. with open(ERROR_LOG_PATH + ERROR_LOG_NAME, 'a') as file:
  3. file.write('\n'.join(lines))
  4. file.write('\n')
  5. file.close()

This worked and the result was a single line of text in a text document expressing a specific error I wanted to record.

The issue came when I realised I was duplicating the with open call throughout my code. So I attempted to convert the write open call to a custom function like this:

  1. def write_to_log(lines):
  2. with open(ERROR_LOG_PATH + ERROR_LOG_NAME, 'a') as file:
  3. file.write('\n'.join(lines))
  4. file.write('\n')
  5. file.close()

Then I call the write to log function each time I need to pass it the list to write like this:

  1. write_to_log([ERROR TITLE: user ' + str(user_id) + ' ' + firstName + ' ' + lastName + ' dataframe is empty for this user])

Now however, I get weird output within the text file. instead of readable text I get:

奇怪的 write()/文件行为

I then commented out the change and reverted back to a write file call per error text call. but I seem to be getting the error no matter what now.

Scope:

I am writing and executing the code via VSCode on a mac. the text file is going to a file location on a network NAS, and I have tried to open the file on various machines and with various text editors (as I thought it may be a NotePad issue on my windows machine), but the bad characters remain.

I am clearly doing something stupid. all assistance greatly appreciated.

Kind regards

Duncan

答案1

得分: 1

  1. 你在使用上下文管理器时调用了file.close(),但你不需要这样做,因为文件会自动关闭。
  2. 尝试指定编码方式。
  1. def write_to_log(lines):
  2. with open(ERROR_LOG_PATH + ERROR_LOG_NAME, 'a', encoding='utf-8') as file:
  3. file.write('\n'.join(lines) + '\n')
英文:
  1. Youre calling file.close() while using context manager, but you dont need to because file will be closed automatically
  2. Try to specify the encoding
  1. def write_to_log(lines):
  2. with open(ERROR_LOG_PATH + ERROR_LOG_NAME, 'a', encoding='utf-8') as file:
  3. file.write('\n'.join(lines) + '\n')
  4. </details>
  5. # 答案2
  6. **得分**: 0
  7. ```python
  8. def write_to_log(lines):
  9. with open(ERROR_LOG_PATH + ERROR_LOG_NAME, 'a') as file:
  10. file.write('\n'.join(lines))
  11. file.write('\n')
  12. file.close()
  13. user_id = 1
  14. firstName ='hello'
  15. lastName = 'goodbye'
  16. ERROR_LOG_PATH = 'rest'
  17. ERROR_LOG_NAME ='test'
  18. lines = ['ERROR TITLE: user ' + str(user_id) + ' ' + firstName + ' ' + lastName + ' dataframe is empty for this user']
  19. write_to_log(lines) # 在此处传递了行
英文:

Your code seems to be working if this is how you intended

  1. def write_to_log(lines):
  2. with open(ERROR_LOG_PATH + ERROR_LOG_NAME, &#39;a&#39;) as file:
  3. file.write(&#39;\n&#39;.join(lines))
  4. file.write(&#39;\n&#39;)
  5. file.close()
  6. user_id = 1
  7. firstName =&#39;hello&#39;
  8. lastName = &#39;goodbye&#39;
  9. ERROR_LOG_PATH = &#39;rest&#39;
  10. ERROR_LOG_NAME =&#39;test&#39;
  11. lines = [&#39;ERROR TITLE: user &#39; + str(user_id) + &#39; &#39; + firstName + &#39; &#39; + lastName + &#39; dataframe is empty for this user&#39;]
  12. write_to_log(lines) //passed lines here

OUTPUT

  1. ERROR TITLE: user 1 hello goodbye dataframe is empty for this user

huangapple
  • 本文由 发表于 2023年7月4日 22:50:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76613807.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定