我正在使用Python打开文本文档:
text_file = open("Output.txt", "w")
text_file.write("Purchase Amount: " 'TotalAmount')
text_file.close()
Run Code Online (Sandbox Code Playgroud)
我想将字符串变量的值替换TotalAmount为文本文档.有人可以让我知道怎么做吗?
当我尝试在Windows控制台中打印Unicode字符串时,出现UnicodeEncodeError: 'charmap' codec can't encode character ....错误.我认为这是因为Windows控制台不接受仅Unicode字符.最好的方法是什么??在这种情况下,有什么方法可以让Python自动打印而不是失败?
编辑: 我正在使用Python 2.5.
注意: @ LasseV.Karlsen回答带有复选标记有点过时(从2008年开始).请谨慎使用下面的解决方案/答案/建议!!
截至今天(2016年1月6日),@ JFSebastian答案更为相关.
我在Windows 7上使用Python 2.6
我从这里借了一些代码: Python,Unicode和Windows控制台
我的目标是能够在Windows控制台中显示uft-8字符串.
显然在python 2.6中
sys.setdefaultencoding函数()
不再受支持
但是,在我尝试使用它之前,我写了reload(sys),它神奇地没有错误.
此代码不会出错,但会显示有趣的字符而不是日文文本. 我相信问题是因为我没有成功更改Windows控制台的代码页.
这些是我的尝试,但它们不起作用:
reload(sys)
sys.setdefaultencoding('utf-8')
print os.popen('chcp 65001').read()
sys.stdout.encoding = 'cp65001'
Run Code Online (Sandbox Code Playgroud)
也许您可以使用win32console来更改代码页? 我尝试了我链接的网站上的代码,但它也从win32console中出错..也许该代码已经过时了.
这是我的代码,这不是错误,但打印有趣的字符:
#coding=<utf8>
import os
import sys
import codecs
reload(sys)
sys.setdefaultencoding('utf-8')
sys.stdout = codecs.getwriter('utf8')(sys.stdout)
sys.stderr = codecs.getwriter('utf8')(sys.stderr)
#print os.popen('chcp 65001').read()
print(sys.stdout.encoding)
sys.stdout.encoding = 'cp65001'
print(sys.stdout.encoding)
x = raw_input('press enter to continue')
a = '???????'#.decode('utf8')
print a
x = raw_input()
Run Code Online (Sandbox Code Playgroud) 我正在处理一些大的 txt 文件(有些大约 3 GB),每当我必须检查 txt 文件时,消息“文件大小超出配置的限制(2.56 MB),代码洞察功能不可用”出现在顶部文件中,我尝试通过转到Help->Edit自定义属性然后在打开的文件中添加下一行代码来更改文件大小
idea.max.content.load.filesize=500000
Run Code Online (Sandbox Code Playgroud)
问题是,即使关闭并重新打开 PyCharm 后也会出现相同的消息,我需要做其他事情吗?只需编写该行代码就足以更改文件大小?,它不需要像普通代码一样运行?如果是这样,由于没有出现该选项,我该如何运行它?
在test.py我有
print('?????? ???')
Run Code Online (Sandbox Code Playgroud)
与cmd正常工作
> python test.py
?????? ???
Run Code Online (Sandbox Code Playgroud)
使用Git Bash出错
$ python test.py
Traceback (most recent call last):
File "test.py", line 2, in <module>
print('\u041f\u0440\u0438\u0432\u0435\u0442 \u043c\u0438\u0440')
File "C:\Users\raksa\AppData\Local\Programs\Python\Python36\lib\encodings\cp1252.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-5: character maps to <undefined>
Run Code Online (Sandbox Code Playgroud)
有谁知道通过Git Bash执行 python 代码时出错的原因?
我在解析这个网站时遇到问题:http : //fm4-archiv.at/files.php?cat=106
正如您在上面的屏幕截图中看到的那样,我的 chrome 浏览器正确显示了变音符号。但是在其他页面上(例如:http : //fm4-archiv.at/files.php?cat = 105),变音没有正确显示,如下面的屏幕截图所示:

元 HTML 标记在页面上定义了以下字符集:
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
Run Code Online (Sandbox Code Playgroud)
我使用 python requests 包来获取 HTML,然后使用 Beautifulsoup 来抓取所需的数据。我的代码如下:
r = requests.get(URL)
soup = BeautifulSoup(r.content,"lxml")
Run Code Online (Sandbox Code Playgroud)
如果我打印编码 ( print(r.encoding),结果是UTF-8. 如果我在控制台上输出数据时手动将编码更改为ISO-8859-1或cp1252通过调用r.encoding = ISO-8859-1没有任何更改。这也是我的主要问题。
r = requests.get(URL)
r.encoding = 'ISO-8859-1'
soup = BeautifulSoup(r.content,"lxml")
Run Code Online (Sandbox Code Playgroud)
仍然会在我的 python IDE 的控制台输出中显示以下字符串:
Der Wildlöwenpfleger
Run Code Online (Sandbox Code Playgroud)
相反,它应该是
Der Wildlöwenpfleger
Run Code Online (Sandbox Code Playgroud)
如何更改我的代码以正确解析变音符号?
我收到UnicodeEncodeError: 'charmap' codec can't encode character '\u200b' in position 756: character maps to error while running this code::
from bs4 import BeautifulSoup
import requests
r = requests.get('https://stackoverflow.com').text
soup = BeautifulSoup(r, 'lxml')
print(soup.prettify())
Run Code Online (Sandbox Code Playgroud)
输出是:
Traceback (most recent call last):
File "c:\Users\Asus\Documents\Hello World\Web Scraping\st.py", line 5, in <module>
print(soup.prettify())
File "C:\Users\Asus\AppData\Local\Programs\Python\Python38\lib\encodings\cp1252.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u200b' in position 756: character maps to <undefined>
Run Code Online (Sandbox Code Playgroud)
我在 vs 代码中使用 python 3.8.1 和 UTF-8。如何解决这个问题?
我正在尝试使用 python 对我下载的 Instagram 数据进行排序,数据是一个 json 文件,但表情符号和其他非文本字符的编码方式我不明白,例如:
json 文件将包含:
\u00e2\u009c\u008c\u00f0\u009f\u0096\u00a4\u00f0\u009f\u008d\u0095\u00f0\u009f\u008e\u00b6\u00f0\u009f\u00a4\u00af. 在 Instagram 应用程序上显示的是:
?
或 json: \u00e2\u0080\u0099. Instagram:('撇号)
我尝试使用u"string"并在此处、此处和此处找到了类似的问题,但没有一个是在 python 中的,也没有向我提供任何有用的详细信息。
一个月前我遇到了这个Github:https://github.com/taraslayshchuk/es2csv
我在Linux ubuntu中通过pip3安装了这个软件包.当我想使用这个包时,我遇到了这个包适用于python2的问题.我深入研究了代码,很快就发现了问题.
for line in open(self.tmp_file, 'r'):
timer += 1
bar.update(timer)
line_as_dict = json.loads(line)
line_dict_utf8 = {k: v.encode('utf8') if isinstance(v, unicode) else v for k, v in line_as_dict.items()}
csv_writer.writerow(line_dict_utf8)
output_file.close()
bar.finish()
else:
print('There is no docs with selected field(s): %s.' % ','.join(self.opts.fields))
Run Code Online (Sandbox Code Playgroud)
代码检查了unicode,这在python3中是不必要的.因此,我将代码更改为下面的代码.因此,该软件包在Ubuntu 16下正常运行.
for line in open(self.tmp_file, 'r'):
timer += 1
bar.update(timer)
line_as_dict = json.loads(line)
# line_dict_utf8 = {k: v.encode('utf8') if isinstance(v, unicode) else v for k, v in line_as_dict.items()}
csv_writer.writerow(line_as_dict)
output_file.close()
bar.finish()
else:
print('There …Run Code Online (Sandbox Code Playgroud) 当我尝试从“url”中抓取一些带有 Finish-Names 的文本时,出现上述错误。我尝试过的解决方案和相应的错误,在代码中注释如下。我既不知道如何解决这些问题,也不知道确切的问题是什么。我是 Python 初学者。任何帮助表示赞赏。
我的代码:
from lxml import html
import requests
page = requests.get('url')
site = page.text # ERROR -> 'charmap' codec can't encode character '\x84' in
# position {x}: character maps to <undefined>
# site = site.encode('utf-8', errors='replace') # ERROR -> can't concat str to bytes
# site = site.encode('ascii', errors='replace') # ERROR -> can't concat str to bytes
with open('url.txt', 'a') as file:
try:
file.write(site + '\n')
except Exception as err:
file.write('an ERROR occured: ' + str(err) …Run Code Online (Sandbox Code Playgroud)