m0_56302292 2022-12-06 18:14 采纳率: 76.5%
浏览 9
已结题

python字符串转化

问题遇到的现象和发生背景
import xlrd as xd
data =xd.open_workbook ('豆瓣电影数据.xls') #打开excel表所在路径
sheet = data.sheet_by_name('豆瓣电影数据')  #读取数据,以excel表名来打开
d1 = []
for r in range(sheet.nrows): #将表中数据按行逐步添加到列表中,最后转换为list结构
    data1 = []
    for c in range(sheet.ncols):
        data1.append(sheet.cell_value(r,c))
    d1.append([i for i in data1 if i and i !=''])

print(d1)

我想要达到的结果

img


图片中数字不加引号,不要字符型

  • 写回答

1条回答 默认 最新

  • ShowMeAI 2022-12-06 18:48
    关注

    望采纳

    import xlrd as xd
    data =xd.open_workbook ('豆瓣电影数据.xls') #打开excel表所在路径
    sheet = data.sheet_by_name('豆瓣电影数据')  #读取数据,以excel表名来打开
    d1 = []
    for r in range(sheet.nrows): #将表中数据按行逐步添加到列表中,最后转换为list结构
        data1 = []
        for c in range(sheet.ncols):
            data1.append(sheet.cell_value(r,c))
        d1.append([i for i in data1 if i and i !=''])
    
    for item in d1:
        item[0] = int(item[0])
        item[2] = int(item[2])
    
    print(d1)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 12月16日
  • 已采纳回答 12月8日
  • 创建了问题 12月6日