我正在编写一个Python文件,需要读入几个不同类型的文件.我正在for line in f使用后逐行阅读文件f = open("file.txt", "r").
这似乎并不适用于所有文件.我的猜测是一些文件以不同的编码结束(例如\ r \n与仅\ r \n).我可以读取整个文件并在\ r \n上执行字符串拆分,但这是非常昂贵的,我宁愿不会.有没有办法让Python的readline方法识别出两种行尾变化?
当我为我创建的类调用一个对象的新实例时,我的一个类实例就被覆盖了.为什么会这样?示例如下.
我的课程定义如下:
class my_class:
attribute = ""
examples = []
children = []
d = {}
def __init__(self, attribute, e):
self.attribute = attribute
self.examples = e
for ex in self.examples:
self.d[ex[-1]] = self.d.get(ex[-1], 0) + 1
Run Code Online (Sandbox Code Playgroud)
我正在制作一个初始实例:
root = my_class(some_attribute, data)
Run Code Online (Sandbox Code Playgroud)
然后,我创建另一个实例:
child = my_class(different_attribute, root.examples[somewhere_1:somewhere_2])
Run Code Online (Sandbox Code Playgroud)
最后,我的初始"root"现在与"child"在某种程度上相同,其中"root"应该保持不变.为什么是这样!?
python ×2