为什么file.readalllines(filename)在从csv读取时会删除前导零? [英] Why does file.readalllines(filename) remove leading zeros when read from csv?

查看:127
本文介绍了为什么file.readalllines(filename)在从csv读取时会删除前导零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用File.ReadAllLines(FileName)读取CSV文件(5MB)。在我的本地系统上它可以很好地工作,但在客户系统上它会删除该行的前导零。没有涉及第三方模块,我无法理解这种行为,因为它一直是字符串。



I'm reading a CSV-File (5MB) with File.ReadAllLines(FileName). On my local system it works perfectly but on the customers system it removes the leading zeros of the line. There are no third party modules involved and I can't understand the behavior since it's plain string all the time.

private static string getCSVLine(string value, string[] _csvFileArray)
        {
            string csvLine = null;

            foreach (string line in _csvFileArray)
            {
                if (!line.StartsWith("#"))
                {
                    string[] csvLineSplit = line.Split(';');
                    if (value == csvLineSplit[0])
                    {
                        csvLine = line.Replace("\"", null);                   
                        return csvLine;
                    }
                }
            }
            return null;
        }





它作为服务运行。此方法中的值是正确的,需要匹配行拆分的第一个值。但所有行开头没有匹配的零。



CSV的示例行:



0000148542; 234258; 1000000010;n; 2003;Cologne;姓名,姓氏; 0; 19461123



编辑:标题具有误导性......当以字符串数组保存时,前导零被删除。原始csv文件保持不变。



我尝试过:



在我自己的系统上测试过。正在正确读取以零开头的行。两个系统之间的区别在于我正在使用Windows 7,客户在Windows Server 2008 R2上运行该服务。



It's running as a service. The value in this method is correct and needs to match the first value of the line split. But all lines beginning with zeros are not being matched.

Example line of the CSV:

0000148542;234258;1000000010;"n";2003;"Cologne";"Name, Lastname";0;19461123

The title was misleading.. the leading zeros are being removed when held in a string array. The original csv file keeps untouched.

What I have tried:

Tested on my own system. Lines beginning with zeros are being read correctly. Differences between the two systems is that I'm working on Windows 7 and the customer runs the service at a Windows Server 2008 R2.

推荐答案

该代码不会删除前导零 - 它不能,因为它所做的全部由分号拆分,并删除双引号。

所以看看之前和之后会发生什么:以及值参数来自何处从。如果它是从整数生成的,则本地区域性设置可能会生成与您期望或在系统上获得的字符串不同的字符串。例如,它可能包含数千个分隔符,这会导致不匹配。



但它不是那个代码!
That code won't remove the leading zeros - it can't because all it does is split out by semicolons, and remove double quotes.
So look at what happens before and after this: and at where the "value" parameter comes from. If that is being generated from an integer, then local culture settings may produce a different string to that you expect or get on your system. For example, it may include thousands separators, which would cause a non-match.

But it isn't that code!


这篇关于为什么file.readalllines(filename)在从csv读取时会删除前导零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆