英文:
Strange write()/File behaviour
问题
lines = ['错误标题:用户 ' + str(user_id) + ' ' + firstName + ' ' + lastName + ' 数据框为空']
with open(ERROR_LOG_PATH + ERROR_LOG_NAME, 'a') as file:
file.write('\n'.join(lines))
file.write('\n')
file.close()
def 写入日志(lines):
with open(ERROR_LOG_PATH + ERROR_LOG_NAME, 'a') as file:
file.write('\n'.join(lines))
file.write('\n')
file.close()
写入日志(['错误标题:用户 ' + 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:
lines = ['ERROR TITLE: user ' + str(user_id) + ' ' + firstName + ' ' + lastName + ' dataframe is empty for this user']
with open(ERROR_LOG_PATH + ERROR_LOG_NAME, 'a') as file:
file.write('\n'.join(lines))
file.write('\n')
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:
def write_to_log(lines):
with open(ERROR_LOG_PATH + ERROR_LOG_NAME, 'a') as file:
file.write('\n'.join(lines))
file.write('\n')
file.close()
Then I call the write to log function each time I need to pass it the list to write like this:
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:
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
- 你在使用上下文管理器时调用了file.close(),但你不需要这样做,因为文件会自动关闭。
- 尝试指定编码方式。
def write_to_log(lines):
with open(ERROR_LOG_PATH + ERROR_LOG_NAME, 'a', encoding='utf-8') as file:
file.write('\n'.join(lines) + '\n')
英文:
- Youre calling file.close() while using context manager, but you dont need to because file will be closed automatically
- Try to specify the encoding
def write_to_log(lines):
with open(ERROR_LOG_PATH + ERROR_LOG_NAME, 'a', encoding='utf-8') as file:
file.write('\n'.join(lines) + '\n')
</details>
# 答案2
**得分**: 0
```python
def write_to_log(lines):
with open(ERROR_LOG_PATH + ERROR_LOG_NAME, 'a') as file:
file.write('\n'.join(lines))
file.write('\n')
file.close()
user_id = 1
firstName ='hello'
lastName = 'goodbye'
ERROR_LOG_PATH = 'rest'
ERROR_LOG_NAME ='test'
lines = ['ERROR TITLE: user ' + str(user_id) + ' ' + firstName + ' ' + lastName + ' dataframe is empty for this user']
write_to_log(lines) # 在此处传递了行
英文:
Your code seems to be working if this is how you intended
def write_to_log(lines):
with open(ERROR_LOG_PATH + ERROR_LOG_NAME, 'a') as file:
file.write('\n'.join(lines))
file.write('\n')
file.close()
user_id = 1
firstName ='hello'
lastName = 'goodbye'
ERROR_LOG_PATH = 'rest'
ERROR_LOG_NAME ='test'
lines = ['ERROR TITLE: user ' + str(user_id) + ' ' + firstName + ' ' + lastName + ' dataframe is empty for this user']
write_to_log(lines) //passed lines here
OUTPUT
ERROR TITLE: user 1 hello goodbye dataframe is empty for this user
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论