我有一个Excel文件
Arm_id DSPName DSPCode HubCode PinCode PPTL
1 JaVAS 01 AGR 282001 1,2
2 JaVAS 01 AGR 282002 3,4
3 JaVAS 01 AGR 282003 5,6
Run Code Online (Sandbox Code Playgroud)
我想在表单中保存一个字符串Arm_id,DSPCode,Pincode.此格式是可配置的,即它可能会更改为DSPCode,Arm_id,Pincode.我将格式保存在列表中
FORMAT = ['Arm_id', 'DSPName', 'Pincode']
Run Code Online (Sandbox Code Playgroud)
如果可配置,我如何阅读具有提供名称的特定列的内容FORMAT.
这是我试过的.目前我能够阅读文件中的所有内容
from xlrd import open_workbook
wb = open_workbook('sample.xls')
for s in wb.sheets():
#print 'Sheet:',s.name
values = []
for row in range(s.nrows):
col_value = []
for col in range(s.ncols):
value = (s.cell(row,col).value)
try : value = str(int(value))
except : pass
col_value.append(value)
values.append(col_value)
print …Run Code Online (Sandbox Code Playgroud) 码:
import urllib2 as u
import os as o
inn = 'dword.txt'
w = open(inn)
z = w.readline()
b = w.readline()
c = w.readline()
x = w.readline()
m = w.readline()
def Dict(Let, Mod):
global str
inn = 'dword.txt'
den = 'definitions.txt'
print 'reading definitions...'
dell =open(den, 'w')
print 'getting source code...'
f = u.urlopen('http://dictionary.reference.com/browse/' + Let)
a = f.read(800)
print 'writing source code to file...'
f = open("dic1.txt", "w")
f.write(a)
f.close()
j = open('defs.txt', 'w')
print 'finding definition is source …Run Code Online (Sandbox Code Playgroud) 如果我们__str__在类中定义方法:
class Point():
def __init__(self, x, y):
self.x = x
self.y = y
def __str__(self, key):
return '{},{}'.format(self.x, self.y)
Run Code Online (Sandbox Code Playgroud)
所以我们可以立即将其对象转换为str:
a = Point(1, 1)
b = str(a)
print(b)
Run Code Online (Sandbox Code Playgroud)
但据我所知,没有这样的__tuple__魔术方法,所以我不知道如何定义一个可以传递的类,tuple()以便我们可以立即将其对象转换为元组.
我有一个文件,其中包含以下格式的数据:请注意这是一个示例,它的外观,实际文件包含2行以上
1 30 5
2 64 4
Run Code Online (Sandbox Code Playgroud)
我在文件中读取,将文本转换为整数,并将它们存储到列表中.这是通过以下代码完成的:
file = open("dataFile.txt", "r")
items = []
for line in file:
line = map(int,line.split()) #convert the text data to integers
items.append(line) #add text data to list
Run Code Online (Sandbox Code Playgroud)
列表的当前格式如下:
[[1, 30, 5], [2, 64, 4]]
Run Code Online (Sandbox Code Playgroud)
我需要将列表列表转换为字典.怎么会这样做呢?
字典键应该是第一个元素
我想从几个不同的元素创建一个元组,其中一个是列表,但我希望在创建元组时将此列表转换为单个元素.
a = range(0,10)
b = 'a'
c = 3
tuple_ex = (a,b,c)
Run Code Online (Sandbox Code Playgroud)
tuple_ex中存储的值为:([0,1,2,3,4,5,6,7,8,9],'a',3)
我希望存储在tuple_ex中的值是:(0,1,2,3,4,5,6,7,8,9,'a',3)
有一种简单的方法可以做到这一点,还是我需要编码?
我正在尝试将元组列表转换为元组元组,我所做的事情是错误的,请帮助解决这个问题
list1= [('2', '297'), ('6', '297')]
for x in list1:
print("inside fn")
print(x)
tuple_of_tuples+=tuple(x)
print(tuple_of_tuples)
Run Code Online (Sandbox Code Playgroud)
输出=('2', '297', '6', '297')
我想要输出为(('2', '297'), ('6', '297'))
>>> list=['a','b']
>>> tuple=tuple(list)
>>> list.append('a')
>>> print(tuple)
('a', 'b')
>>> another_tuple=tuple(list)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object is not callable
Run Code Online (Sandbox Code Playgroud)
为什么我不能将列表'list'转换为元组?