使用ezread读取.csv文件时遇到问题

时间:2019-03-23 10:50:48

标签: matlab

我试图用ezread https://de.mathworks.com/matlabcentral/fileexchange/11026-ezread读取csv文件,但出现以下问题:

  

使用textscan时出错   第二个输入必须是包含至少一个>说明符或文字字段的格式字符向量。

     

ezread错误(第66行)   data = textscan(fid,format_str,'delimiter',file_delim,'headerlines',1);

我按如下方式调用该函数:

tmpName = '/path/file.csv';
structRead = ezread(tmpName, 'r');

我用 isfile()检查了 tmpName 是否正确,所以它是正确的路径。
我文件的前两行具有以下格式:

a,b,c,d
1,2,e,f

您知道问题可能在哪里吗?

1 个答案:

答案 0 :(得分:0)

您应该使用import random import json encounters = { "enemy" : { 1 : "zombie", 2 : "wolf", 3 : "wraith" }, "chest" : { 1 : "chest", 2 : "weapons_chest", }, "npc" : { 1 : "villager", }, "furniture" : { 1 : "bed" }, "nothing" : { 1 : "" } } area = { "spawner" : { "spawn" : { "name" : "Spawn", "interact" : "", "solved" : False, "connects" : { 1 : "alleyway_1" } } }, "alley" : { "alleyway_1" : { "name" : "Alleyway 1", "interact" : "", "solved" : False, "connects" : { 1 : "spawn", 2 : "alleyway_2", 3 : "hall" } }, "alleyway_2" : { "name" : "Alleyway 2", "interact" : "", "solved" : False, "connects" : { 1 : "alleyway_1" } } } } def random_encounters(): print("random encounters") encounters_keys = ["enemy", "chest", "npc", "furniture", "nothing"] weights_a = [0.3, 0.1, 0.05, 0.05, 0.5] for q, w in area.items(): for a, s in area[q].items(): encounter_a = random.choices(encounters_keys, weights_a)[0] encounter_b = random.choice(list(encounters[encounter_a].keys())) area[q][a]["interact"] = encounters[encounter_a][encounter_b] print(json.dumps(area, indent=2)) random_encounters() 而不是ezread。但是,它不会识别逗号importdata上的分隔符。因此,您需要添加额外的行:

,

结果是:

tmpName = importdata('/path/file.csv');
structRead = split(a, ',')