当我使用模块fileinput循环遍历一组gzip压缩文件的行时,如下所示:
for line in fileinput.FileInput(files=gzipped_files,openhook=fileinput.hook_compressed):
Run Code Online (Sandbox Code Playgroud)
那些行是字节字符串而不是文本字符串.
当使用模块gzip时,可以通过使用'rt'而不是'rb'打开文件来防止这种情况:http://bugs.python.org/issue13989
模块fileinput是否有类似的修复,所以我可以让它返回文本字符串而不是字节字符串?我尝试添加mode ='rt',但后来我收到此错误:
ValueError: FileInput opening mode must be one of 'r', 'rU', 'U' and 'rb'
Run Code Online (Sandbox Code Playgroud) 我可以像这样读取二进制文件的第一个字节:
with open(my_binary_file,'rb') as f:
f.read(1)
Run Code Online (Sandbox Code Playgroud)
但是我该如何使用 fileinput 模块来做到这一点呢?如果我运行这段代码:
import fileinput
with fileinput.FileInput(my_binary_file,'rb') as f:
f.read(1)
Run Code Online (Sandbox Code Playgroud)
然后我得到这个错误:
AttributeError: 'FileInput' object has no attribute 'read'
Run Code Online (Sandbox Code Playgroud)
是否有类似于 fileinput 的模块,它允许我读取多个二进制文件的字节/字符而不是行?
编辑:读取二进制文件的一行并循环它不是一个选项,因为二进制文件很大并且不包含换行符。
我有一个如下所示的文件,我想保留在第三个字段上具有最高值的第一个和第二个字段之间的组合(带有箭头的那些,实际文件中不包含箭头)。
1 1 10
1 1 12 <-
1 2 6 <-
1 3 4 <-
2 4 32
2 4 37
2 4 39
2 4 40 <-
2 45 12
2 45 15 <-
3 3 12
3 3 15
3 3 17
3 3 19 <-
3 15 4
3 15 9 <-
4 17 25
4 17 28
4 17 32
4 17 36 <-
4 18 4 <-
Run Code Online (Sandbox Code Playgroud)
为了有这样的输出:
1 1 12
1 2 6 …Run Code Online (Sandbox Code Playgroud) 我想上传一个ASCII文件.这曾经在Python 2中工作:
ftp = ftplib.FTP('ftp.domain.com')
ftp.login('domain.com',password)
ftp.cwd('subdirectory')
ftp.storlines('STOR ' + 'file.htm', open('file.htm','r'))
ftp.close()
Run Code Online (Sandbox Code Playgroud)
但是,在Python 3中它返回此错误:
File "/usr/local/lib/python3.3/ftplib.py", line 497, in storlines
if buf[-1] in B_CRLF: buf = buf[:-1]
TypeError: Type str doesn't support the buffer API
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
有一段时间我一直想知道的事情.在此列表理解中,拆分是执行一次还是多次?
l = [line.split()[i] for i in indexes]
Run Code Online (Sandbox Code Playgroud)
我目前以这种方式列出这样的理解:
l = line.rstrip().split()
l = [l for i in indexes]
Run Code Online (Sandbox Code Playgroud)
但我不确定,是否有必要.除了是/否答案,我肯定想知道,我可以通过CPU分析或阅读一些文档来了解自己.谢谢.
我是C++的新手.在Python 3中,只要一对位为11,我就可以将字符串'ABC'转换为这样的选定位并打印:
s = 'ABC'
for i, char in enumerate(s):
for j in range(4):
if ord(char) >> 2*j & 0b11 == 3:
print(i, char, ord(char), j, ord(char) >> 2*j & 0b11)
Run Code Online (Sandbox Code Playgroud)
哪个回报:
2 C 67 0 3
Run Code Online (Sandbox Code Playgroud)
我如何在C++中做同样的事情; 即如何识别字符'C'的第1位和第2位是11?我目前有这个代码:
//#include <string>
//#include <bitset>
#include <iostream>
//using namespace std;
int main(){
const int bits_in_byte = 8;
std::string s = "ABC";
for (std::size_t i = 0; i < s.size(); ++i)
{
for (int j = 0; j < 4; ++j) {
std::cout …Run Code Online (Sandbox Code Playgroud) 如何使用模块从一组 png 图像制作 mp4 视频imageio?我试过这个:
import imageio
import glob
writer = imageio.get_writer('test.mp4', fps=20)
for png_path in glob.glob('*.png'):
im = imageio.imread(png_path),
writer.append_data(im[:, :, 1])
writer.close()
Run Code Online (Sandbox Code Playgroud)
我也试着更换im[:, :, 1]用im。我究竟做错了什么?我很高兴使用另一个模块。
在 Python 3 中,这些陈述是正确的:
ord('A') == 65
chr(65) == 'A'
bin(65) == '0b1000001'
hex(65) == '0x41'
int(b'1000001',2) == 65
Run Code Online (Sandbox Code Playgroud)
给定整数i = 65,如何在Python 3中以二进制模式打开文件并将字节写入01000001文件?
尽管很简单,但我找不到重复的问题。
在压缩流中随机访问数据
我知道Biopyton 1.60的Bio.bgzf模块,其中:
支持读写BGZF文件(Blocked GNU Zip Format),这是GZIP的一种变体,具有高效的随机访问,最常用作BAM文件格式的一部分和tabix.它在内部使用Python的zlib库,并提供一个简单的接口,如Python的gzip库.
但对于我的用例,我不想使用那种格式.基本上我想要一些东西,它模仿下面的代码:
import gzip
large_integer_new_line_start = 10**9
with gzip.open('large_file.gz','rt') as f:
f.seek(large_integer_new_line_start)
Run Code Online (Sandbox Code Playgroud)
但是本机zlib.net提供的效率可以提供对压缩流的随机访问.如何利用Python中的随机访问功能?
在gnuplot中,我可以这样做以获得平方图:
set size square
Run Code Online (Sandbox Code Playgroud)
matplotlib中的等效项是什么?我已经试过了:
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
plt.rcParams['backend'] = 'TkAgg'
x = [0, 0.2, 0.4, 0.6, 0.8]
y = [0, 0.5, 1, 1.5, 2.0]
colors = ['k']*len(x)
plt.scatter(x, y, c=colors, alpha=0.5)
plt.axes().set_aspect('equal', adjustable='datalim')
plt.xlim((0,2))
plt.ylim((0,2))
plt.grid(b=True, which='major', color='k', linestyle='--')
plt.savefig('{}.png'.format(rsID), dpi=600)
plt.close()
plt.clf()
Run Code Online (Sandbox Code Playgroud)
我得到一个正方形网格,但是情节本身不是正方形。如何使x范围从0变到2,并使绘图呈正方形?

python ×8
python-3.x ×4
binaryfiles ×2
file-io ×2
gzip ×2
aspect-ratio ×1
bit ×1
byte ×1
c++ ×1
c++11 ×1
ftp ×1
indexing ×1
matplotlib ×1
mp4 ×1
plot ×1
scatter-plot ×1
sorting ×1
string ×1
uniq ×1
unix ×1
video ×1