【问题标题】:How to read value in CSV file如何读取 CSV 文件中的值
【发布时间】:2017-09-17 05:21:45
【问题描述】:

在某个场景方面需要您的帮助,即我必须从数据库中获取一个值,例如“42258258.2300”并将其转换为“42,258,258.00”值。

再次需要检查该值是否存在于下载的 CSV 文件中。

我使用下面的代码从 CSV 文件中读取,但没有得到输出。请帮我解决这个问题。

String strFile = "outputfile.csv";
FileInputStream fstream = new  FileInputStream(strFile);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null)   {
String ss = "$42,699,561.00";
if(strLine.contains(ss)){
 System.out.println ("Success"); }
}
in.close();

【问题讨论】:

  • 是否可以提供一些 CSV 文件的示例行?在不知道货币在文件中的格式的情况下,我无法说出为什么 .contains() 没有找到任何命中。
  • 您的问题不清楚。 outputfile.csv 是什么?它包含什么? “42258258.2300”、“42,258,258.00”还是“42,699,561.00 美元”?您的代码读取 csv 文件的所有行,并期望其中一行包含“ss”字符串,这意味着其中一行应包含“$42,699,561.00”。
  • 而您没有看到“成功”这一事实显然意味着 csv 文件不包含您要查找的美元字符串。
  • @luc14n0 outputfile.csv 是从站点下载的文件。这将包含一些像这样的值 $42,699,561.00。是的,其中一行包含此值。文件包含此值我不知道如何在此处附加文件对不起

标签: java selenium selenium-webdriver selenium-grid


【解决方案1】:

如果你打印 strLine,你会得到什么?你真的得到字符串了吗?

这段代码对我有用

public void getFileInformation(){
        String strFile = "/Users/myUser/Documents/outputfile.csv";
        BufferedReader br = null;
        String strLine = "";
        String ss = "$42,699,561.00";           
        try {
            br = new BufferedReader(new FileReader(strFile));                
            while ((strLine= br.readLine()) != null) {
                if(strLine.contains(ss)){
                    System.out.println ("Success"); 
                }else{
                    System.out.println (strLine);
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                 } catch (IOException e) {
                    e.printStackTrace();
                 }
            }
        }
    }

但是内容“$42,699,561.00”;应该确实存在于文件中

【讨论】:

  • hi @Jose Pedroza 执行了脚本,但即使存在“$42,699,561.00”值也会打印 else 语句。但是当我用“$”执行时,这会给出“成功”。我不知道你为什么能帮助我?
  • 很抱歉,我整个周末都在外面,如果你仍然有疑问,我现在会尽力帮助你,当你说“当我用“$”执行时,这会给出“成功””您是在文件中还是在字符串中使用“$”?
  • 没关系。在文件中检查“$ 42,699,561.00”值,但没有得到执行:(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-30
  • 2019-05-27
  • 2019-01-30
  • 1970-01-01
  • 2022-01-06
  • 2017-06-06
相关资源
最近更新 更多