使用php逐行解析csv文件
我有一个包含数据的 csv 文件 第 1 行是这样的标题
id fname lname email date code pid
,每行然后在每个字段名称下方都有数据
3232456454 mike strong [email protected] 11/8/11 0:00 AU 2540
,当我使用文本编辑器看到此 csv 时,我看到它如下。
3232456454,mike,strong,[email protected],11/8/11 0:00,AU,2540
87876548788,bob,cool,[email protected],11/8/11 0:00,RY,2148
23498765,nike,tick,[email protected],11/8/11 0:00,TE,5240
现在我想使用 php 读取文件,并且希望数据的格式与文本文件完全相同。我将按原样将这些数据用于其他目的。我不想用逗号分隔,所以我不能使用 fgetcsv。我尝试使用 php file() 打开它,但是当我循环它时,我没有看到正确的数据。有人可以提出一些建议吗?
如果我这样做,它会在数组中打印,每个元素取一个值,我不想要这个。
$csv = array();
$lines = file('sample.csv', FILE_IGNORE_NEW_LINES);
foreach ($lines as $key => $value)
{
$csv[$key] = str_getcsv($value);
}
print_r($csv)
我尝试使用谷歌代码中的 parsecsv 库,但这并不是真正必要的,因为我想要逐行数据。
我需要忽略输出中的第一行(标题)。
如果我能得到这样的输出,这将解决我的
Array
(
[id,fname,lname,email,,data,code,pid] => 3232456454,mike,strong,[email protected],11/8/11 0:00,AU,2540
)
Array
(
[id,fname,lname,email,,data,code,pid] => 87876548788,bob,cool,[email protected],11/8/11 0:00,RY,2148
)
问题
I have a csv file with data
row 1 is header like this
id fname lname email date code pid
each row then has data below each field name
3232456454 mike strong [email protected] 11/8/11 0:00 AU 2540
and when i see this csv using text editor, i see it as below.
3232456454,mike,strong,[email protected],11/8/11 0:00,AU,2540
87876548788,bob,cool,[email protected],11/8/11 0:00,RY,2148
23498765,nike,tick,[email protected],11/8/11 0:00,TE,5240
Now i want to read the file using php and i want the data to be exactly same format as text file. I will use this data for other purpose as it is. I don't want to split at commas so i cannot use fgetcsv. I tried by just opening it using php file() but when i loop it, i don't see the proper data. Can some one please throw some suggestions?
if I do this way, it prints in the array with each element taking one value and I don't want this..
$csv = array();
$lines = file('sample.csv', FILE_IGNORE_NEW_LINES);
foreach ($lines as $key => $value)
{
$csv[$key] = str_getcsv($value);
}
print_r($csv)
I tried using parsecsv library from google code but this is not really necessary as i want the data line by line.
I need to ignore the first line(header) in the output.
If i can get the output like this, this will solve my issues
Array
(
[id,fname,lname,email,,data,code,pid] => 3232456454,mike,strong,[email protected],11/8/11 0:00,AU,2540
)
Array
(
[id,fname,lname,email,,data,code,pid] => 87876548788,bob,cool,[email protected],11/8/11 0:00,RY,2148
)
regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这将按原样获取文件,无需任何转换或拆分或任何其他操作,只需一个长字符串。
如果您想将其拆分为每行的数组,只需执行
此操作即可。没有循环,没有
getcsv
,只有file()
。但即使您说您对解析 CSV 不感兴趣,您实际上正在处理 CSV,因此我会尽快解析它并再次输出,甚至在必要时返回 CSV。
如果无法正确识别行结尾,请在脚本顶部尝试
ini_set('auto_detect_line_endings', true);
。请参阅此处的演示:http://codepad.viper-7.com/t31IEv
This will get the file as is without any conversions or splitting or anything, just one long string.
If you want to split it into an array for each line, simply do
and nothing else. No looping, no
getcsv
, justfile()
.But even if you say you're not interested in parsing the CSV, you are dealing with a CSV so I'd parse it as soon as possible and output it again, even back to CSV if necessary.
If line endings aren't properly recognized, try
ini_set('auto_detect_line_endings', true);
on top of your script.See here for a demo: http://codepad.viper-7.com/t31IEv
使用 fopen 和 fgets 代替,并使用您所在行的计数器,这样您就可以忽略第一行:
Use fopen and fgets instead, and use a counter of which line you're at so you can ignore the first:
如果无论您做什么,都只能使用 fgets() 或 fgetcsv() 获取 CSV 文件的最后一行,只需尝试将文件内容复制/粘贴到新文件中,然后尝试使用它即可。
我指的是 @Jay 似乎遇到的问题:似乎如果该文件是使用 Excel 2011/Mac 创建的,则其结构无法正确解释。
有关详细信息,请参阅此:哪种编码可以在 Mac 和 Windows 上使用 Excel 正确打开 CSV 文件?
If no matter what you do you can only get the last line of your CSV file using fgets() or fgetcsv(), simply try to copy/paste the content of your file to a new one and try using it instead.
I'm referring to the issue that @Jay seems to had: It seems that if the file was created with Excel 2011/Mac, its structure is not correctly interpreted.
See this for more info: Which encoding opens CSV files correctly with Excel on both Mac and Windows?