GitOPEN's Home.

日常踩坑实录

Word count: 1,213 / Reading time: 6 min
2020/01/30 Share

前言

在coding的过程中,难免会遇到各种问题,我把踩过/填满的坑记录下来,一方面有助于今后查缺补漏,也希望能够帮到其它小伙伴。 —— by GitOPEN

碎片

过滤字符串中的emoji表情和符号[Python]

最近在抓取了几十万条微博数据,目的是对其进行情感分析,这就需要过滤掉内容中表情等特殊符号。在Google了一圈以后,发现很多方法过滤的效果不好,因此自己记录一下,如何更加全面的过滤掉表情符号。这个方法综合使用了正则表达式和emoji库。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import emoji


def filter_emoji(text):
"""
过滤表情。
Author: GitOPEN
"""
try:
regex = re.compile(u'[\U00010000-\U0010ffff]')
except re.error:
regex = re.compile(u'[\uD800-\uDBFF][\uDC00-\uDFFF]')

text = regex.sub(u'', text)

return ''.join(word for word in text if word not in emoji.UNICODE_EMOJI)

# 测试:随机输入3个人物emoji,3个自然,3个食物,3个符号,3个物体,3个旗帜
emoji_text = '🤡👨‍👮世界,我是周董🙉🦎🦀,🍑🍔🍤你好吗?❤️㊗️🆘我很好,日子过得还算不错!📡🕯🔨你呢?🇧🇷🇻🇬🇺🇸'
print(filter_emoji(emoji_text))

git 设置代理

由于众所周知的原因,git速度实在是蜗速,只有几KB/s。挂上小飞机✈️,按照下面设置,速度瞬间飞起!当然你得有一架好飞机~😀
如果你的✈️非得不够高,那么请参考之前我的推荐《快的飞起小飞滴~》

1
2
git config --global http.proxy 'socks5://127.0.0.1:1080'
git config --global https.proxy 'socks5://127.0.0.1:1080'

取消git代理:

1
2
git config --global --unset http.proxy
git config --global --unset https.proxy

Python/Django生成CSV文件内容乱码

在Django视图函数中生成CSV文件,用微软妹子家的Excel打开会乱码,解决方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def book_price(request):
import csv, codecs

titles = ['明朝那些事儿', '围城', '北京法源寺', '平凡的世界', '丰乳肥臀']
response = HttpResponse(content_type='text/csv')
# 解决乱码
response.write(codecs.BOM_UTF8)
response['Content-Disposition'] = 'attachment; filename=book_price.csv'
writer = csv.writer(response)
writer.writerow(['title', 'price'])
for (title, price) in zip(titles, range(69, 99)):
print(title, price)
writer.writerow([title, price])

return response

使用Python写CSV文件时,也会出现相同的问题,解决方法:

1
2
3
4
5
6
7
8
import csv, codecs
f=open('temp.csv','w')
# 解决乱码
f.write(codecs.BOM_UTF8)
writer = csv.writer(f)
writer.writerow(['图书','出版社','价格'])
writer.writerow(['明朝那些事儿','机械工业出版社','146.99'])
f.close()

使用Python读取CSV文件时,也会出现乱码的问题,解决方法,指定目标文件的编码方式:

1
2
3
4
5
import csv
with open('data.csv', 'r', encoding='utf-8') as f:
f_csv = csv.reader(f)
for row in f_csv:
print(row)

macOS 移除Google的GoogleSoftwareUpdateAgent更新程序

查看该程序有没有在运行:

1
defaults read com.google.Keystone.Agent

如果看到的是Domain com.google.Keystone.Agent does not exist,则没有运行,无需移除;

如果是一长串吧哩巴拉,那么需要移除。移除方法:

1
sudo /Library/Google/GoogleSoftwareUpdate/GoogleSoftwareUpdate.bundle/Contents/Resources/GoogleSoftwareUpdateAgent.app/Contents/Resources/ksinstall --uninstall

移除完毕后可再次检测一遍。

Firefox开启Youtube的4k视频

macOS上Firefox默认不能看Youtube的4k及以上分辨率视频,调整ff的设置即可打开。

  • 在Firefox中打开about:config
  • 搜索media.mediasource.webm.enabled
  • 将其值设置为true即可

macOS fish shell 终端设置代理

安装 fish shell 后,iterm2 或 Terminal 用起来不能更爽。但是如果能够让终端中的网络走代理,那么更是爽之又爽。

1
vim ~/.config/fish/config.fish

输入以下设置,端口号按照自己代理的配置进行更改:

1
2
3
set -x https_proxy http://127.0.0.1:7890
set -x http_proxy http://127.0.0.1:7890
set -x all_proxy socks5://127.0.0.1:7891

matplotlib 后台画图报错 NSInternalInconsistencyException

更新于 2020/01/30 10:52:00

最近在对APS数据集进行分析,需要使用matplotlib网状结构和趋势图,但是由于数据量较大,我加入了多线程进行操作,出现报错(中间有好多行,省略):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2020-01-29 16:41:59.365 python[78453:3976088] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'NSWindow drag regions should only be invalidated on the Main Thread!'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff2c5d18ab __exceptionPreprocess + 250
1 libobjc.A.dylib 0x00007fff62842805 objc_exception_throw + 48
2 CoreFoundation 0x00007fff2c5fa30c -[NSException raise] + 9
3 AppKit 0x00007fff2974a538 -[NSWindow(NSWindow_Theme) _postWindowNeedsToResetDragMarginsUnlessPostingDisabled] + 310
4 AppKit 0x00007fff29731ed5 -[NSWindow _initContent:styleMask:backing:defer:contentView:] + 1416

75 python 0x000000010592e345 PyObject_Call + 101
76 python 0x0000000105a5d7a6 t_bootstrap + 70
77 libsystem_pthread.dylib 0x00007fff63db4e65 _pthread_start + 148
78 libsystem_pthread.dylib 0x00007fff63db083b thread_start + 15
)
libc++abi.dylib: terminating with uncaught exception of type NSException
fish: 'python make_networks7.py' terminated by signal SIGABRT (Abort)

主体错误为:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'NSWindow drag regions should only be invalidated on the Main Thread!'

解决方法如下:

import matplotlib.pyplot as plt以后,设置matplotlib在后台运行,不显示前台GUI,设置方法如下:

1
matplotlib.use('agg')

或者

1
plt.switch_backend('agg')


未完待续,越踩坑越🐂🍺



欣慰帮到你 一杯热咖啡
【奋斗的Coder!】企鹅群
【奋斗的Coder】公众号
CATALOG
  1. 1. 前言
  2. 2. 碎片
    1. 2.1. 过滤字符串中的emoji表情和符号[Python]
    2. 2.2. git 设置代理
    3. 2.3. Python/Django生成CSV文件内容乱码
    4. 2.4. macOS 移除Google的GoogleSoftwareUpdateAgent更新程序
    5. 2.5. Firefox开启Youtube的4k视频
    6. 2.6. macOS fish shell 终端设置代理
    7. 2.7. matplotlib 后台画图报错 NSInternalInconsistencyException
    8. 2.8. 未完待续,越踩坑越🐂🍺