繁体   English   中英

如何从文本文件中提取数据?

[英]How to extract data from text files?

因此,我有一组文件,我需要从中提取数据并写入新的txt文件,而且我不确定如何使用Python执行此操作。 以下是样本数据。 我正在尝试从NSF组织,文件和摘要中提取零件。

标题:CRB:神秘鲸的濒危种群的遗传多样性:线粒体DNA和历史人口统计学类型:奖项NSF组织:DEB最新修订日期:1991年8月1日
文件:a9000006

奖项编号:9000006奖项说明:持续资助
产品经理:Scott Collins
环境生物学的债务划分
生物直接生物学
开始日期:1990年6月1日
保质期:1992年11月30日(估计)。 :$ 179720(预估)研究者:Stephen R.Palumbi(现任主要研究者)赞助商:夏威夷大学马诺阿分校2530 Dole Street Honolulu,HI 968222225 808 / 956-7800

NSF计划:1127系统和人口BIOLO Fld应用:0000099其他应用NEC
61生命科学生物学
程序编号:9285,摘要:

          Commercial exploitation over the past two hundred years drove the great    
          Mysticete whales to near extinction.  Variation in the sizes of populations  
          prior to exploitation, minimalpopulation size during exploitation and 
          current population sizes permit analyses of the effects of differing levels 
          of exploitation on species with different biogeographical distributions and 
          life-history characteristics. 

您没有给我太多帮助,而是从txt文件读取输入文件。 这是用Java编写的,希望您会知道如何将其存储在某种数组中

import java.util.Scanner;

import java.io.*;

public class ClockAngles{

public static void main (String [] args) throws IOException {

Scanner reader = null;
String input = "";
try {
    reader = new Scanner (new BufferedReader (new FileReader("FilePath")));

    while (reader.hasNext()) {
        input = reader.next();
        System.out.print(input);

    } 
}

finally {
        if (reader != null) {
        reader.close();
        }
}

Python代码

#!/bin/env python2.7

# Change this to the file with the time input
filename = "filetext"

storeData = []

class Whatever:
def __init__(self, time_str):
    times_list = time_str.split('however you want input to be read')
    self.a = int(times_list[0]) 
    self.b = int(times_list[1]) 
    self.c = int(times_list[2]) 
    # prints the data
    def __str__(self):
    return str(self.a) + " " + str(self.b) + " " + str(self.c)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM