为什么file.readalllines(filename)在从csv读取时会删除前导零?

编程入门 行业动态 更新时间:2024-10-09 02:28:12
本文介绍了为什么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读取时会删除前导零?

本文发布于:2023-11-03 23:56:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1556422.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:前导   时会   readalllines   file   csv

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!