Python 变量代入,指定某一行截取输出字段
c=2
执行:F = ('n'.join(open('C:\\Users\\Administrator\\Desktop\\ID.txt','r').readlines()[str(b):str(c)]))
运行结果:TypeError: slice indices must be integers or None or have an __index__ method
执行:F = ('n'.join(open('C:\\Users\\Administrator\\Desktop\\ID.txt','r').readlines().decode('gbk')[b:c]))
运行结果:AttributeError: 'list' object has no attribute 'decode'
执行:F = ('n'.join(open('C:\\Users\\Administrator\\Desktop\\ID.txt','r', encoding='gbk').readlines()[b:c]))
运行结果:TypeError: 'encoding' is an invalid keyword argument for this function
各种方法都是过了还是不能正常运行,求大神帮我解惑 展开
编码的话要看你py文件的编码和读取文件的编码
a=1
b=10
f=''.join(open("C:\\Users\\Administrator\\Desktop\\ID.txt").readlines()[a:b])
print f
你第一誉族森行就基本写对了,就是穗拍list切片index只能是整形或空庆亩
a=1
b=10
f=''.join(open("C:\\Users\\Administrator\\Desktop\\ID.txt").readlines()[a:b])
print f
使用连接竖搭世符: +
world = "World"
print "Hello " + world + " ! "
2. 使用占位符来内插
world = "World"
print "Hello %s !" % world
3.如何使用函数
li = ['my','name','is','bob']
mystr = ' '.join(li)
print mystr
4.上面的语句中字符串是作为参数传入的余肢,可以直接用变量替换:
begin_date = '2012-04-06 00:00:00'
end_date = '2012-04-06 23:59:59'
select * from usb where time between to_date(begin_date,'YYYY-MM-DD HH24:MI:SS') and to_date(end_date,'YYYY-MM-DD HH24:MI:SS')
py3下式encode
我用的是py2.X,能否用py2.X代码帮我解决一下?谢谢了