小编Suk*_*lra的帖子

Python摆脱字节b''

import save

string = ""

with open("image.jpg", "rb") as f:
    byte = f.read(1)
    while byte != b"":
        byte = f.read(1)
        print ((byte))
Run Code Online (Sandbox Code Playgroud)

我得到的字节如下:

b'\x00'

我怎么摆脱这个b''

假设我想将字节保存到列表中,然后再将该列表另存为同一图像.我该怎么办?

谢谢!

python python-3.x

20
推荐指数
2
解决办法
5万
查看次数

将partial_fit与Scikit Pipeline一起使用

你如何调用partial_fit()包裹在Pipeline()中的scikit-learn分类器?

我正在尝试使用以下方法构建一个可递增训练的文本分类器SGDClassifier:

from sklearn.linear_model import SGDClassifier
from sklearn.pipeline import Pipeline
from sklearn.feature_extraction.text import HashingVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.multiclass import OneVsRestClassifier

classifier = Pipeline([
    ('vectorizer', HashingVectorizer(ngram_range=(1,4), non_negative=True)),
    ('tfidf', TfidfTransformer()),
    ('clf', OneVsRestClassifier(SGDClassifier())),
])
Run Code Online (Sandbox Code Playgroud)

但我AttributeError试着打电话classifier.partial_fit(x,y).

它支持fit(),所以我不明白为什么partial_fit()不可用.是否可以内省管道,调用数据转换器,然后直接调用partial_fit()我的分类器?

python scikit-learn

12
推荐指数
2
解决办法
5261
查看次数

如何在python中追加子列表的元素

我有一个表格列表:

list = [[3, 1], [3, 2], [3, 3]]
Run Code Online (Sandbox Code Playgroud)

我想将它分成两个列表,一个列表中每个子列表的x值,另一个列表中每个子列表的y值.

我目前有这个:

x = y = []
for sublist in list:
    x.append(sublist[0])
    y.append(sublist[1])
Run Code Online (Sandbox Code Playgroud)

但是这会返回这个,我不知道为什么:

x = [3, 1, 3, 2, 3, 3]
y = [3, 1, 3, 2, 3, 3]
Run Code Online (Sandbox Code Playgroud)

python list

5
推荐指数
1
解决办法
3250
查看次数

在python中读取csv文件时出现错误“没有这样的文件或目录”

目前,我正在尝试使用python中的csv模块读取csv文件。当我运行下面的代码时,我得到一个错误,指出该文件不存在。我的第一个猜测是,可能我将文件保存在错误的位置,或者需要为pyton提供文件路径。目前,我已将文件保存在C:\ Documents and Settings \ eag29278 \ My Documents \ python测试代码\ test_satdata.csv中。

一侧说明,请注意,将模式设置为“ rb”(读取二进制)是正确的做法。

import csv
with open('test_satdata.csv', 'rb') as csvfile:
    satreader = csv.reader(csvfile, delimiter=' ', lineterminator=" ")
    for row in satreader:
        print ', '.join(row)
Run Code Online (Sandbox Code Playgroud)

这是错误代码。

Traceback (most recent call last):
File "C:/Python27/test code/test csv parse.py", line 2, in <module>
    with open('test_satdata.csv', 'rb') as csvfile:
IOError: [Errno 2] No such file or directory: 'test_satdata.csv'
Run Code Online (Sandbox Code Playgroud)

python csv filepath

5
推荐指数
1
解决办法
2万
查看次数

列表理解返回值加[无,无,无],为什么?

我正在研究理解.我得到了print(x)部分(我认为.它打印了传递'in'测试的x的值)但是为什么它之后也会返回None列表呢?

>>> g
['a', 'x', 'p']

>>> [print(x) for x in g]
a
x
p
[None, None, None] #whats this? 
Run Code Online (Sandbox Code Playgroud)

python python-3.x

5
推荐指数
2
解决办法
4301
查看次数

Matplotlib 和日期时间错误 date2num 函数 AttributeError

我正在尝试绘制一个以日期时间对象列表作为一个轴的图表。我在网上搜索了一下,看来我应该调用 date2num 函数。但是,当我调用它时,我收到属性错误。

这是我写的代码:

listOfDates
[datetime.date(2013, 8, 20), datetime.date(2013, 8, 21)]
dates = mathplotlib.dates.date2num(listOfDates)
Run Code Online (Sandbox Code Playgroud)

这是我得到的错误:

Traceback (most recent call last):
File "<pyshell#30>", line 1, in <module>
  dates = matplotlib.dates.date2num(listOfDates)
AttributeError: 'module' object has no attribute 'dates'
Run Code Online (Sandbox Code Playgroud)

非常感谢

python datetime matplotlib

4
推荐指数
1
解决办法
7383
查看次数

如何解释Python切片语法列表中的第一个冒号[:: - 1]

我最近阅读了有关如何反转序列的代码片段

>> l = [1,2,3,4,5,6]
>> print l[::-1]
Run Code Online (Sandbox Code Playgroud)

产量

>> [6,5,4,3,2,1]
Run Code Online (Sandbox Code Playgroud)

如何解释括号中的第一个冒号?

python list

4
推荐指数
1
解决办法
2412
查看次数

使用for循环在列表中添加值

我是Python的新手,我无法解决为什么这不起作用.

number_string = input("Enter some numbers: ")

# Create List
number_list = [0]

# Create variable to use as accumulator
total = 0

# Use for loop to take single int from string and put in list
for num in number_string:
    number_list.append(num)

# Sum the list
for value in number_list:
    total += value

print(total)
Run Code Online (Sandbox Code Playgroud)

基本上,我希望用户输入123例如然后得到1和2和3之和.

我收到此错误,不知道如何打击它.

Traceback (most recent call last):
  File "/Users/nathanlakes/Desktop/Q12.py", line 15, in <module>
    total += value
TypeError: unsupported operand type(s) for +=: 'int' and 'str'
Run Code Online (Sandbox Code Playgroud)

我只是在我的教科书中找不到答案,并且不明白为什么我的第二个for循环不会迭代列表并将值累加到total.

python python-3.x

3
推荐指数
1
解决办法
6万
查看次数

Python绘图

我对绘图有疑问.我想在范围之间绘制一些数据:

3825229325678980.07868125697521248069633804173619323825229325678980.078681262584097479512892231994772

但是我收到以下错误:

Attempting to set identical bottom==top results
in singular transformations; automatically expanding.
bottom=3.82522932568e+15, top=3.82522932568e+15
Run Code Online (Sandbox Code Playgroud)

我应该如何在这里增加小数点来解决问题?

python matplotlib

2
推荐指数
1
解决办法
2598
查看次数

realloc()问题,没有分配

我正在尝试读一个字符串

char *string=malloc(sizeof(char));
char *start_string=string; //pointer to string start
while ((readch=read(file, buffer, 4000))!=0){ // read
    filelen=filelen+readch; //string length
    for (d=0;d<readch;d++)
        *start_string++=buffer[d]; //append buffer to str
    realloc(string, filelen); //realloc with new length
Run Code Online (Sandbox Code Playgroud)

有时这会崩溃并出现以下错误:

   malloc: *** error for object 0x1001000e0: pointer being realloc'd was not allocated
Run Code Online (Sandbox Code Playgroud)

但有时不是,我不知道如何解决它.

c c++ unix linux

1
推荐指数
1
解决办法
1232
查看次数

标签 统计

python ×9

python-3.x ×3

list ×2

matplotlib ×2

c ×1

c++ ×1

csv ×1

datetime ×1

filepath ×1

linux ×1

scikit-learn ×1

unix ×1