如何将具有逗号的文本文件转换为由这些逗号分隔的数组?例如,我使用
General:
Request URL: http://192.168.0.23/login.php
Request Method: POST
Status Code: 200 OK
Remote Address: 192.168.0.23:80
Referrer Policy: no-referrer-when-downgrade
Response Headers:
Connection: Keep-Alive
Content-Encoding: gzip
Content-Length: 135
Content-Type: text/html; charset=UTF-8
Date: Sat, 10 Nov 2018 22:01:13 GMT
Keep-Alive: timeout=5, max=100
Server: Apache/2.4.10 (Raspbian)
Vary: Accept-Encoding
Request Headers:
POST /login.php HTTP/1.1
Host: 192.168.0.23
Connection: keep-alive
Content-Length: 41
Cache-Control: max-age=0
Origin: http://192.168.0.23
Upgrade-Insecure-Requests: 1
Content-Type: application/x-www-form-urlencoded
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,
image/webp,image/apng,*/*;q=0.8
Referer: http://192.168.0.23/
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9
Form Data:
username: admin
password: letmein
Login: Login
这显然会显示以下内容:
var = importdata(filename)
disp(var)
然后如何使'please, help, me'
成为数组,以便可以使用类似于var
的东西提取单个单词?
答案 0 :(得分:2)
使用strsplit
(要求≥R2013a)或split
(要求≥R2016b)将字符数组var{1}
拆分为一个单元格数组,该单元格数组在单独的单元格中包含这些单词。
v = strsplit(var{1},', '); %or v = split(var{1},' ,');
现在v{1}
,v{2}
和v{3}
分别给'please'
,'help'
和'me'
。
使用var{1}
是因为必须从importdata
返回单元格数组var
。如果var
不是单元格数组而是字符数组,那么您将不会在disp
的输出中得到单引号。