Matlab读取csv字符串数组(Matlab read csv string array)
我有一个名为Book2.csv的逗号分隔数据集我想提取内容。 内容是496024x1字符串数组(正常,海王星,蓝精灵)。
我试过:
[text_data] = xlsread('Book2.csv');
但它只是输出了一个text_data空数组?
尝试csvread时
M = csvread('Book2.csv')
??? Error using ==> dlmread at 145
Mismatch between file and format string.
Trouble reading number from file (row 1, field 1) ==>
norma
Error in ==> csvread at 54
m=dlmread(filename, ',', r, c);
我收到这个错误。 有人可以帮忙吗?
I have a comma seperated dataset called Book2.csv I want to extract the contents. The contents are a 496024x1 array of strings (normal, neptune, smurf).
I tryed:
[text_data] = xlsread('Book2.csv');
But it just outputed a text_data empty array?
When trying csvread
M = csvread('Book2.csv')
??? Error using ==> dlmread at 145
Mismatch between file and format string.
Trouble reading number from file (row 1, field 1) ==>
norma
Error in ==> csvread at 54
m=dlmread(filename, ',', r, c);
I get this error. Can anyone help?
原文:https://stackoverflow.com/questions/11507501
更新时间:2020-07-25 21:07
最满意答案
在我的头脑中,这应该完成工作。 但可能不是最好的方法。
fid = fopen(your file); //open file
//read all contents into data as a char array
//(don't forget the `'` to make it a row rather than a column).
data = fread(fid, '*char')';
fclose(fid);
//This will return a cell array with the individual
//entries for each string you have between the commas.
entries = regexp(data, ',', 'split');
Off the top of my head this should get the job done. But possibly not the best way to do it.
fid = fopen(your file); //open file
//read all contents into data as a char array
//(don't forget the `'` to make it a row rather than a column).
data = fread(fid, '*char')';
fclose(fid);
//This will return a cell array with the individual
//entries for each string you have between the commas.
entries = regexp(data, ',', 'split');
2016-01-23
相关问答
尽管这个问题接近于完全重复,但@NathanG提供的链接(即使用xlsread )中提出的解决方案只是解决您的问题的一种可能方式。 链接中的作者还建议使用textscan ,但不提供任何关于如何做的信息,所以我想我会在这里添加一个示例: %# First we need to get the header-line
fid1 = fopen('file.csv', 'r');
Header = fgetl(fid1);
fclose(fid1);
%# Convert Header to cel
...
我会用StringIO : try:
# for Python 2.x
from StringIO import StringIO
except ImportError:
# for Python 3.x
from io import StringIO
import csv
scsv = """text,with,Polish,non-Latin,lettes
1,2,3,4,5,6
a,b,c,d,e,f
gęś,zółty,wąż,idzie,wąską,dr
...
更新:从Spark 2.2.x开始,最后有一种使用Dataset的正确方法。 import org.apache.spark.sql.{Dataset, SparkSession}
val spark = SparkSession.builder().appName("CsvExample").master("local").getOrCreate()
import spark.implicits._
val csvData: Dataset[String] = spark.sparkConte
...
编辑 - 对不起,我误解了你的问题。 更新了我的回答。 您可以将整个csv作为字符串读取,然后将所需的列转换为其他类型,如下所示: df = pd.read_csv('/path/to/file.csv', dtype=str)
# example df; yours will be from pd.read_csv() above
df = pd.DataFrame({'A': ['1', '3', '5'], 'B': ['2', '4', '6'], 'C': ['x', 'y', 'z']
...
我的Matlab给你的代码提供了不同的错误信息。 >> rows_attack = strcmp(T(:,2),'attack')
rows_attack =
0
>> T(rows_attack,2) = 1
Right hand side of an assignment into a table must be another table or a cell array.
>> T(rows_attack,2)
ans =
empty 0-by-1 tabl
...
你必须帮助genfromtxt一点: data = np.genfromtxt('home_data.csv',
dtype=[int,float],delimiter=',',names=True,
converters={0: lambda b:(b.decode().strip('"'))})
每个字段都以字节形式收集。 float(b'1 \ n')返回1.0,但float(b'“8210”')给出错误。 转换器选项允许为每个字段(此处为字段0)定义一个执行正确转换的函数,此处转换
...
在我看来,最明智的方法是使用正则表达式来匹配正确的模式(引号和逗号对): %// Read lines as strings
fid = fopen('input.txt', 'r');
C = textscan(fid, '%s', 'Delimiter', '\n');
fclose(fid);
%// Tokenize each line using a regular expression
C = cellfun(@(x){[x{:}]}, regexp(C{:}, '"([^"]*)"
...
首先我想说我会用Map而不是两个数组,但是我会告诉你一个使用两个数组的解决方案。 你接近解决方案。 其中一个问题是scanner.next()只会读取输入,直到第一个空格为止。 这就是你需要使用scanner.nextLine()的原因 。 此方法读取完整的行。 代码可能看起来像这样: 两个阵列的解决方案 Scanner sc = new Scanner(System.in);
System.out.print("Please enter name of student: ");
String n
...
我们使用StringIO myFakeFile = StringIO.StringIO()
wtr = csv.DictWriter( myFakeFile, headings )
...
myFakeFile.getvalue()
通常有效。 We use StringIO for this myFakeFile = StringIO.StringIO()
wtr = csv.DictWriter( myFakeFile, headings )
...
myFakeFile.getvalue(
...
在我的头脑中,这应该完成工作。 但可能不是最好的方法。 fid = fopen(your file); //open file
//read all contents into data as a char array
//(don't forget the `'` to make it a row rather than a column).
data = fread(fid, '*char')';
fclose(fid);
//This will return a cell array
...