简体   繁体   English

读取CSV文件并将数据转换为python列表。 ValueError:无法将字符串转换为float:

[英]Reading a CSV file and converting data into python lists. ValueError: could not convert string to float:

I have a CSV file containing adjacency matrix of a network. 我有一个包含网络邻接矩阵的CSV文件。 When I tried to convert this data into python list of lists using the following code: 当我尝试使用以下代码将此数据转换为列表的python列表时:

def main(): 
    csvfile= open('airport_CnToCn_ajc.csv','rU')
    reader = csv.reader(csvfile,delimiter=" ")
    alldata = list(reader)
    print alldata
    numData=[[float(i) for i in row] for row in alldata]
    print numData

I got an error 我有一个错误

ValueError: could not convert string to float ,"Afghanistan","Albania","Algeria","American

because first row and first column of my csv file denotes source and destination of the network edges. 因为我的csv文件的第一行和第一列表示网络边缘的源和目标。

Is there an alternate to overcome this error other than modifying the CSV file by deleting first row and first column. 除了通过删除第一行和第一列来修改CSV文件之外,是否还有其他方法可以克服此错误。 If I follow the later approach, I would lose track of the nodes. 如果遵循后面的方法,我将无法跟踪节点。

If you know the first row and column are not needed, you could skip them in iteration. 如果您知道不需要第一行和第一列,则可以在迭代中跳过它们。

def main(): 
    with open('airport_CnToCn_ajc.csv','rU') as csvfile:
      reader = csv.reader(csvfile)
      alldata = list(reader)[1:]
    print alldata
    numData=[[float(i) for i in row[1:]] for row in alldata]
    print numData

Or, to reduce some copies (likely unneeded): 或者,以减少一些副本(可能不需要):

reader_iter = csv.reader(csvfile)
next(reader_iter)
alldata = list(reader_iter)

You have two options here: 您在这里有两个选择:

  1. You can skip the first row and the first column: 您可以跳过第一行和第一列:

     def main(): with open('airport_CnToCn_ajs.csv', 'rU') as csvfile: reader = csv.reader(csvfile, delimiter=',') next(reader) # Skip first row rows = list(reader) data = [float(i) for i in row[1:] for row in rows] # skip first column 
  2. You can use a DictReader : 您可以使用DictReader

     def main(): with open('airport_CnToCn_ajs.csv', 'rU') as csvfile: reader = csv.DictReader(csvfile, delimiter=',') rows = list(reader) data = [] for row in rows: i = [] i.append(row['columna']) # Your data columns i.append(row['columnb']) data.append(map(float, i)) # convert to float 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将csv文件读取到python ValueError:无法将字符串转换为float - Reading csv file to python ValueError: could not convert string to float ValueError:无法将字符串转换为 CSV 文件中的浮点数 - ValueError: could not convert string to float in a CSV file Python:ValueError:无法将字符串转换为浮点数:读取输入文件以应用随机森林时“隔离” - Python: ValueError: could not convert string to float: 'Isolated' when reading input file for applying random forest Python:ValueError:无法将字符串转换为实时数据浮点数 - Python : ValueError: could not convert string to float in real time data ValueError: 无法将字符串转换为浮点数:'.' Python - ValueError: could not convert string to float: '.' Python Python Barplot ValueError:无法将字符串转换为浮点数: - Python Barplot ValueError: could not convert string to float: Python ValueError:无法将字符串转换为浮点数:“退出” - Python ValueError: could not convert string to float: 'exit' Python ValueError:无法将字符串转换为浮点数 - Python ValueError: could not convert string to float ValueError:无法将字符串转换为浮点数:'F' python - ValueError: could not convert string to float: 'F' python ValueError:无法将字符串转换为float'jpg'Python - ValueError: could not convert string to float 'jpg' Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM