今天早上我很好,真的很难过。我已经从mysql数据库中的数据创建了一个CSV文件,一切正常。但是,上传文件时出现问题,因为它的EOL不正确;它使用\ n而不是\ r \ n。
我可以打开文件并将所有\ n更改为\ r \ n,但是当将字符串转换为数组时,它会添加双引号,而不应该是任何字符。
这是原始CSV
TH,1.00,550300,I,550300-app,5050085022119,5031151000019,5031151013286,20151118,20151118,20151118 ,,, 0.00,GBP,0.00,0.00,0.00," I-Read Ltd" ," GB 162 8111 28"," TOYMASTER LIMITED",GB486083911, TB,1.00,550300,I,550300-app,5031151013286 ,," Kids Stuff Ltd,Putney"," The Exchange"," High Street",Putney,伦敦," SW15 1TW" TD,1.00,550300,I,550300-app,1 ,,, IR1 ,,"环游世界贴纸书",6,1.5000,9.0000,Z,0.00 TD,1.00,550300,I,550300-app,2 ,,, B52 ,," Playful Puppies海报贴纸书",4,1.5000,6.0000,Z,0.00 TD,1.00,550300,I,550300-app,3 ,,, IR6 ,," The Boys Brilliant Sticker Book",4,1.5000,6.0000,Z,0.00 TD,1.00,550300,I,550300-app,4 ,,, IR26 ,,"农场用水的主题油漆",1,1.5000,1.5000,Z,0.00 TD,1.00,550300,I,550300-app,5 ,,, IR36 ,," Doodling and Coloring Dinosaur",1,1.0000,1.0000,Z,0.00 TD,1.00,550300,I,550300-app,6 ,,, IR4 ,,"在移动贴纸书",6,1.5000,9.0000,Z,0.00 TD,1.00,550300,I,550300-app,7 ,,, PW01 ,,"用水涂料",2,0.9000,1.8000,Z,0.00 TD,1.00,550300,I,550300-app,8 ,,, IR30 ,," Jumbo Sticker Book Princess Fun",3,1.0000,3.0000,Z,0.00 TD,1.00,550300,I,550300-app,9 ,,, 30H ,," Activity Fun",3,0.5000,1.5000,Z,0.00 TD,1.00,550300,I,550300-app,10 ,,, IR27 ,," Jumbo Sticker Book Pirate Fun",4,1.0000,4.0000,Z,0.00 TD,1.00,550300,I,550300-app,11 ,,, 1006S ,," Big 1000 Fairy贴纸垫",7,1.5000,10.5000,S,0.20 TD,1.00,550300,I,550300-app,12 ,,, B56 ,,"动物海报贴纸书",2,1.5000,3.0000,Z,0.00 TD,1.00,550300,I,550300-app,13 ,,, IR2 ,,"世界各地的动物贴纸书",9,1.5000,13.5000,Z,0.00 TD,1.00,550300,I,550300-app,14 ,,, IR3 ,,"在City Sticker Book",6,1.5000,9.0000,Z,0.00 TD,1.00,550300,I,550300-app,15 ,,, IR5 ,," The Girls Beautiful Sticker Book",10,1.5000,15.0000,Z,0.00 TD,1.00,550300,I,550300-app,16 ,,, B51 ,," Horses and Ponies海报贴纸书",7,1.5000,10.5000,Z,0.00 TD,1.00,550300,I,550300-app,17 ,,, IR28 ,," Jumbo Sticker Book Fairy Funtime",7,1.0000,7.0000,Z,0.00 TD,1.00,550300,I,550300-app,18 ,,, B53 ,," Cuddly Kittens海报贴纸书",6,1.5000,9.0000,Z,0.00 TD,1.00,550300,I,550300-app,19 ,,, IR14 ,,"在黑暗恐龙王国发光",4,1.2500,5.0000,Z,0.00 TD,1.00,550300,I,550300-app,20 ,,, B57 ,," Numbers海报贴纸书",2,1.5000,3.0000,Z,0.00 TD,1.00,550300,I,550300-app,21 ,,, IR25 ,,"主题涂料与水可爱的宠物",1,1.5000,1.5000,Z,0.00 TD,1.00,550300,I,550300-app,22 ,,, IR29 ,," Jumbo Sticker Book Space Fun",3,1.0000,3.0000,Z,0.00 TD,1.00,550300,I,550300-app,23 ,,, B54 ,,"农场海报贴纸书",1,1.5000,1.5000,Z,0.00 TD,1.00,550300,I,550300-app,24 ,,, 878H-S ,,"在黑暗空间冒险中发光",3,1.2500,3.7500,Z,0.00 TD,1.00,550300,I,550300-app,25 ,,, B50 ,," Huggable Bunnies海报贴纸书",1,1.5000,1.5000,Z,0.00 TD,1.00,550300,I,550300-app,26 ,,, 32H ,," Dot to Dot Fun",4,0.5000,2.0000,Z,0.00 TD,1.00,550300,I,550300-app,27 ,,, 877H-B ,," Ballerina Glitter Sticker book",2,1.2500,2.5000,Z,0.00 TD,1.00,550300,I,550300-app,28 ,,, 31H ,," Coloring Fun",12,0.5000,6.0000,Z,0.00 TD,1.00,550300,I,550300-app,29 ,,, 33H ,," Alphabet Dot to Dot",7,0.5000,3.5000,Z,0.00 TD,1.00,550300,I,550300-app,30 ,,, 878H-A ,,"黑暗外星人和怪物中的光辉",10,1.2500,12.5000,Z,0.00 TD,1.00,550300,I,550300-app,31 ,,, 878H-D ,,"在黑暗恐龙王国发光",8,1.2500,10.0000,Z,0.00 TD,1.00,550300,I,550300-app,32 ,,, 878H-U ,,"在海底黑暗中发光",6,1.2500,7.5000,Z,0.00 TD,1.00,550300,I,550300-app,33 ,,, IR24 ,,"带水动物乐趣的主题涂料",6,1.5000,9.0000,Z,0.00 TX,1.00,550300,我,550300-应用,L,0.20,10.50,2.10,194.30 IT,1.00,550300,I,550300应用内,192.55,10.50,2.10,194.30
这就是生产的东西!
TH,1.00,550300,I,550300-app,5050085022119,5031151000019,5031151013286,20151118,20151118,20151118 ,,, 0.00,GBP,0.00,0.00,0.00," I-Read Ltd" ," GB 162 8111 28"," TOYMASTER LIMITED",GB486083911," TB",1.00,550300,I,550300-app,5031151013286 ,," Kids Stuff Ltd,Putney"," The Exchange"," High Street" ,普特尼,伦敦," SW15 1TW TD",1.00,550300,I,550300-app,1 ,,, IR1 ,,"环游世界贴纸书",6,1.5000,9.0000,Z," 0.00 TD",1.00,550300,I,550300-app,2 ,,, B52 ,," Playful Puppies海报贴纸书",4,1.5000,6.0000,Z," 0.00 TD",1.00,550300,I,550300-app,3 ,,, IR6 ,," The Boys Brilliant Sticker Book",4,1.5000,6.0000,Z," 0.00 TD",1.00,550300,I,550300-app,4 ,,, IR26 ,,"农场用水的主题油漆",1,1.5000,1.5000,Z," 0.00 TD",1.00,550300,I,550300-app,5 ,,, IR36 ,," Doodling and Coloring Dinosaur",1,1.0000,1.0000,Z," 0.00 TD",1.00,550300,I,550300-app,6 ,,, IR4 ,,"在移动贴纸书",6,1.5000,9.0000,Z," 0.00 TD",1.00,550300,I,550300-app,7 ,,, PW01 ,,"用水涂抹",2,0.9000,1.8000,Z," 0.00 TD",1.00,550300,I,550300-app,8 ,,, IR30 ,," Jumbo Sticker Book Princess Fun",3,1.0000,3.0000,Z," 0.00 TD",1.00,550300,I,550300-app,9 ,,, 30H ,,"活动乐趣",3,0.5000,1.5000,Z," 0.00 TD",1.00,550300,I,550300-app,10 ,,, IR27 ,," Jumbo Sticker Book Pirate Fun",4,1.0000,4.0000,Z," 0.00 TD",1.00,550300,I,550300-app,11 ,,, 1006S ,," Big 1000 Fairy贴纸垫",7,1.5000,10.5000,S," 0.20 TD",1.00,550300,I,550300-app,12 ,,, B56 ,,"动物海报贴纸书",2,1.5000,3.0000,Z," 0.00 TD",1.00,550300,I,550300-app,13 ,,, IR2 ,,"动物环游世界贴纸书",9,1.5000,13.5000,Z," 0.00 TD",1.00,550300,I,550300-app,14 ,,, IR3 ,,"在City Sticker Book",6,1.5000,9.0000,Z," 0.00 TD",1.00,550300,I,550300-app,15 ,,, IR5 ,," The Girls Beautiful Sticker Book",10,1.5000,15.0000,Z," 0.00 TD",1.00,550300,I,550300-app,16 ,,, B51 ,," Horses and Ponies海报贴纸书",7,1.5000,10.5000,Z," 0.00 TD",1.00,550300,I,550300-app,17 ,,, IR28 ,," Jumbo Sticker Book Fairy Funtime",7,1.0000,7.0000,Z," 0.00 TD",1.00,550300,I,550300-app,18 ,,, B53 ,," Cuddly Kittens海报贴纸书",6,1.5000,9.0000,Z," 0.00 TD",1.00,550300,I,550300-app,19 ,,, IR14 ,,"在黑暗恐龙王国发光",4,1.2500,5.0000,Z," 0.00 TD",1.00,550300,I,550300-app,20 ,,, B57 ,," Numbers海报贴纸书",2,1.5000,3.0000,Z," 0.00 TD",1.00,550300,I,550300-app,21 ,,, IR25 ,,"主题涂料与水可爱宠物",1,1.5000,1.5000,Z," 0.00 TD",1.00,550300,I,550300-app,22 ,,, IR29 ,," Jumbo Sticker Book Space Fun",3,1.0000,3.0000,Z," 0.00 TD",1.00,550300,I,550300-app,23 ,,, B54 ,,"农场海报贴纸书",1,1.5000,1.5000,Z," 0.00 TD",1.00,550300,I,550300-app,24 ,,, 878H-S ,,"在黑暗空间冒险中发光",3,1.2500,3.7500,Z," 0.00 TD",1.00,550300,I,550300-app,25 ,,, B50 ,," Huggable Bunnies海报贴纸书",1,1.5000,1.5000,Z," 0.00 TD",1.00,550300,I,550300-app,26 ,,, 32H ,," Dot to Dot Fun",4,0.5000,2.0000,Z," 0.00 TD",1.00,550300,I,550300-app,27 ,,, 877H-B ,," Ballerina Glitter Sticker book",2,1.2500,2.5000,Z," 0.00 TD",1.00,550300,I,550300-app,28 ,,, 31H ,,"着色乐趣",12,0.5000,6.0000,Z," 0.00 TD",1.00,550300,I,550300-app,29 ,,, 33H ,," Alphabet Dot to Dot",7,0.5000,3.5000,Z," 0.00 TD",1.00,550300,I,550300-app,30 ,,, 878H-A ,,"黑暗外星人和怪兽中的光辉",10,1.2500,12.5000,Z,&#34 ; 0.00 TD",1.00,550300,I,550300-app,31 ,,, 878H-D ,,"黑暗恐龙王国的发光",8,1.2500,10.0000,Z," 0.00 TD",1.00,550300,I,550300-app,32 ,,, 878H-U ,,"在海底黑暗中发光",6,1.2500,7.5000,Z,&#34 ; 0.00 TD",1.00,550300,I,550300-app,33 ,,, IR24 ,,"带水动物乐趣的主题油漆",6,1.5000,9.0000,Z," 0.00 TX",1.00,550300,I,550300应用内,L,0.20,10.50,2.10," 194.30 IT",1.00,550300,I,550300应用内,192.55,10.50,2.10,194.30
(注意第一列末尾和最后一列开头的附加双引号)
我在mac中使用od命令来读取csv文件,我可以看到该文件已从\ n更改为\ r \ n
这是我使用的php。我一直在寻找答案几个小时,找不到任何与这个实际问题相关的答案。
$csvfile1 = __DIR__.'/csv/' . $jobid . '_tm.csv'; // FILE THAT HAS ALREADY BEEN CREATED
$file_new = fopen ('csv/' . $jobid . '_tmnew.csv','w'); // CREATES NEW EMPTY CSV FILE
$s_contents = file_get_contents($csvfile1); // READS THE CONTENTS OF THE EXISTING FILE
$s_new = str_replace("\n", "\r\n", $s_contents); // REPLACES \N WITH \R\N
$s_tidy = str_getcsv($s_new, ',' , '"', "#"); // CONVERTS STRING $s_new TO ARRAY
fputcsv($file_new, $s_tidy); // PUTS THE ARRAY INTO THE NEW FILE
fclose ($file_new); // SAVES THE FILE
任何可以抛出的光都会受到赞赏
由于
答案 0 :(得分:0)
您应该在转换为csv之前删除所有空格。例如,使用此代码或使用正则表达式从字符串中删除空格。
$s_new = str_replace(["\n"," "], ["\r\n", ""], $s_contents); // REPLACES \N WITH \R\N AND REMOVES WHITESPACES
此代码适用于我。
诚恳