使用jackson-dataformat-csv获取csv的标头

时间:2019-03-15 14:45:11

标签: java csv jackson

我正在使用from tkinter import * from tkinter import ttk from PIL import ImageTk, Image root = Tk() sizeIcons = 30 f1 = ttk.Frame(root, padding=(0, 0, 0, 0)) f1.grid(row=0, column=0, sticky='news') label1 = Label(f1) myimg1 = ImageTk.PhotoImage(Image.open('C:\\Users\\Michael\\Desktop\\align.left.png').resize((sizeIcons, sizeIcons), Image.ANTIALIAS)) label1['image'] = myimg1 label1.place(relx=0.25, rely=0.50, anchor=CENTER) label1.pack() label2 = Label(f1) myimg2 = ImageTk.PhotoImage(Image.open('C:\\Users\\Michael\\Desktop\\align.center.png').resize((sizeIcons, sizeIcons), Image.ANTIALIAS)) label2['image'] = myimg2 label2.place(relx=0.50, rely=0.50, anchor=CENTER) label2.pack() label3 = Label(f1) myimg3 = ImageTk.PhotoImage(Image.open('C:\\Users\\Michael\\Desktop\\align.right.png').resize((sizeIcons, sizeIcons), Image.ANTIALIAS)) label3['image'] = myimg3 label3.place(relx=0.75, rely=0.50, anchor=CENTER) label3.pack() root.mainloop() 库。我要解析from tkinter import * from tkinter import ttk from PIL import ImageTk, Image root = Tk() sizeIcons = 30 def raise_frame(frame): frame.tkraise() f1 = ttk.Frame(root, padding=(0, 0, 0, 0)) f1.grid(row=0, column=0, sticky='news') label1 = Label(f1) myimg1 = ImageTk.PhotoImage(Image.open('C:\\Users\\Michael\\Desktop\\align.left.png').resize((sizeIcons, sizeIcons), Image.ANTIALIAS)) label1['image'] = myimg1 label1.place(relx=0.25, rely=0.50, anchor=CENTER) label1.grid() def a (): raise_frame(f2) next1 = ttk.Button(f1, text='Next', command=a, default='active') next1.grid(sticky=NW, padx=160, pady=320) f2 = ttk.Frame(root, padding=(0, 0, 0, 0)) f2.grid(row=0, column=0, sticky='news') label2 = Label(f2) myimg2 = ImageTk.PhotoImage(Image.open('C:\\Users\\Michael\\Desktop\\align.center.png').resize((sizeIcons, sizeIcons), Image.ANTIALIAS)) label2['image'] = myimg2 label2.place(relx=0.50, rely=0.50, anchor=CENTER) label2.grid() root.mainloop() 文件。我有以下代码:

jackson-dataformat-csv

我想从该文件中获取列名。但是我不知道该怎么做。我尝试:

CSV

但是,它是空对象。我也是这样:

 CsvMapper csvMapper = new CsvMapper();
        CsvSchema csvSchema = csvMapper.typedSchemaFor(Map.class).withHeader();
        List<Map<String, String>> csvRows;
        try {
            MappingIterator<Map<String, String>> it = csvMapper.readerFor(Map.class)
                    .with(csvSchema.withColumnSeparator(';'))
                    .readValues(file.getInputStream());
            csvRows = it.readAll();
        } catch (Exception ex) {
            log.error("Unable to read csv document: ", ex);
        }

我收到错误消息:

csvSchema._columns

很显然,columns对象为空。为什么?如何从csvSchema.column(0) 获取列名数组?

2 个答案:

答案 0 :(得分:1)

在您从CsvSchema获取解析器之前,字段不在MappingIterator

一个工作示例:

CsvSchema schema = (CsvSchema) objectMappingIterator.getParser().getSchema();
        Iterator<CsvSchema.Column> iterator = schema.iterator();
        while (iterator.hasNext()) {
            CsvSchema.Column next = iterator.next();
            next.getName();
        }

答案 1 :(得分:0)

如果需要列名,可以将它们作为Map返回的MappingIterator中的键:

CsvMapper csvMapper = new CsvMapper();
CsvSchema csvSchema = csvMapper.typedSchemaFor(Map.class).withHeader();

MappingIterator<Map<String, String>> it = csvMapper.readerFor(Map.class)
        .with(csvSchema.withColumnSeparator(','))
        .readValues(csvFile);

System.out.println("Column names: " + it.next().keySet());