[英]Python and excel reading files problem
I am sorry if this is a silly question but I have been working on this for hours and I cannot make it work.如果这是一个愚蠢的问题,我很抱歉,但我已经为此工作了几个小时,但我无法让它发挥作用。 Please help!请帮忙!
I have a.txt file that originated from Excel.我有一个源自 Excel 的 .txt 文件。 The file contains strings and numbers but I am only interested in the numbers, which is why I skip the first line and I only read from column 2 on.该文件包含字符串和数字,但我只对数字感兴趣,这就是为什么我跳过第一行而只从第 2 列开始读取。
from numpy import *
I load it into Python doing我将它加载到 Python 中
infile = open('europenewMatrix.txt','r')
infile.readline() # skip the first line
numbers = [line.split(',')[2:] for line in infile.readlines()]
infile.close()
because I need to do computations with this, I convert it into a matrix:因为我需要对此进行计算,所以我将其转换为矩阵:
travelMat = array(numbers)
ok, but this didn't convert the strings into integers, so I manually do it:好的,但这并没有将字符串转换为整数,所以我手动做到了:
for i in xrange(len(numbers)):
for j in xrange(len(numbers)):
travelMat[i,j] = int(self.travelMat[i,j])
#end for
At this point, I was hoping that all my entries would be integers but if I do在这一点上,我希望我所有的条目都是整数,但如果我这样做了
print 'type is',type(self.travelMat[1,2])
the answer is:答案是:
type is <type 'numpy.string_'>
how can I really convert all my entries into integers?我怎样才能真正将所有条目转换为整数? thanks a lot!多谢!
convert the numbers as you read them, before creating the array:在创建数组之前,在读取数字时转换它们:
infile = open('europenewMatrix.txt','r')
infile.readline() # skip the first line
numbers = []
for line in infile:
numbers.append([int(val) for val in line.split(',')[2:]])
infile.close()
travelMat = array(numbers)
If you're working with a csv or csv-like file, use the csv standard library module.如果您使用的是 csv 或类似 csv 的文件,请使用 csv 标准库模块。
from numpy import *
import csv
infile = open('europenewMatrix.txt', 'r')
reader = csv.reader(infile)
reader.next() # skip the first line
numbers = [[int(num) for num in row[2:]] for row in reader]
infile.close()
travelmat = array(numbers)
http://docs.python.org/library/csv.html http://docs.python.org/library/csv.html
if someone has a question that could have the same title but uses real Excel (.xls) files, try this (using module xlrd ):如果有人有一个可能具有相同标题但使用真正的 Excel (.xls) 文件的问题,试试这个(使用模块xlrd ):
import xlrd
import numpy as np
sheet = xlrd.open_workbook('test_readxls.xls').sheet_by_name('sheet1')
n_rows, n_cols = 5,2
data = np.zeros((n_rows, n_cols))
for row in range(n_rows):
for col in range(n_cols):
data[row,col] = float(sheet.cell(row,col).value)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.