如何阅读每一行,然后找到丢失的文件?

时间:2018-02-14 05:17:35

标签: java python

示例输入格式:

>>> a='20'
>>> b='27'
>>> chr(int(a+b,16))
'‧'

输出:

缺少文档格式

Name of the file,  Author,  format type,  id,  content length.
resume,            abc,     pdf,          7,   90
resume,            asc,     doc,          2,   90
resume,            azc,     docx,         3,   90

这是我的方法:从外部txt文件中获取输入(必需)。

pdf

2,3

doc

7,3

那么,我的问题是使用哪种数据结构?读完每一行后。我应该如何处理数据存储? 示例代码将是很有帮助的。提前致谢。

2 个答案:

答案 0 :(得分:1)

解决方案基于这样的前提:“格式类型”字段中的条目中只有一个值。

此解决方案需要使用google guava集合。罐子可以从“https://github.com/google/guava/wiki/Release19”下载

import java.io.BufferedReader;
import java.io.File;
import java.lang.reflect.Array;
import java.util.*;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
public class FileReader {

public void processData() {

Multimap dataMap = readFile();
dataMap.get("format type");
Object[] array =  ((ArrayListMultimap) dataMap).get("format type").toArray();

 System.out.println("Missing formats");
 for (Object entry:array) {
 System.out.println(entry.toString().trim());

 String position= "";
 for(int i=0;i<array.length;i++) {
 if(!entry.toString().equalsIgnoreCase (array[i].toString())) {
 position= position+" "+i;
 }
 }
 System.out.println(position);
 }
 }

  public Multimap readFile() {
  File file = new File("/Users/sree/Desktop/text.txt");
  Multimap<String,String> dataMap =  ArrayListMultimap.create();

  ArrayList<String> al=new ArrayList<String>();
  BufferedReader br;

  try {
  br = new BufferedReader(new java.io.FileReader(file));
  Arrays.stream(br.readLine().split(",")).forEach(s ->al.add(s.trim()));
  String st;
  while ((st = br.readLine()) != null) {

  VariableIcrementor instance = new VariableIcrementor();
  Arrays.stream(st.split(",")).                                      
  forEach(s->dataMap.put(al.get(instance.increment()),s));
  }
  br.close();
  } catch (Exception e) {
  e.printStackTrace();
  }
  return dataMap;
  }

  public static void main(String[] args) {
       FileReader instance = new FileReader();
       instance.processData();
    }
}

变量增量或实现细节

public class VariableIcrementor {

private int i=0;

public int increment() {
    return i++;
}
}

答案 1 :(得分:0)

您可以使用散列图将数据作为格式存储数据,将ID作为值存储。如果你想使用python,这是一个示例:

# Read line from file
with open('filename.txt') as f:
    line = f.readline()
entries = line.split(' ')
# Hashmap to store formats and corresponding ids
formats = {}
# set to store all the ids
ids = set()
for en in entries:
    vals = en.split(',')
    frmt, identifier = vals[2], vals[3]
    # Set of ids for each format
    if frmt not in formats:
        formats[frmt] = set()
    formats[frmt].add(identifier)
    ids.add(identifier)
print("Missing formats")
for frmt in formats:
    print(frmt)
    # Missing formats are set difference between all the ids and the current ids
    print(ids-formats[frmt])