从txt文件中提取特定数据,并附加到csv数据库

时间:2019-02-19 16:30:23

标签: python excel csv append

我有3000多个文本文件,我想从中提取部分数据。每个文本文件都用空格分隔,我希望从第0列和第29列的前3行提取数据并将其附加到新的csv文件中。

我已经写了一些代码来提取我想要的数据并制作一个新的csv文件,但是我不确定如何将数据附加到现有的csv中。理想情况下,我想遍历文本文件,将2列中的3条相关数据行附加到csv数据库中。谁能建议我该怎么做?

# opens text file and sorts delimiters
with open('Text_file_1.txt', 'r') as in_file:
    lines = in_file.read().splitlines()
    stripped = [line.replace(","," ").split() for line in lines]
    grouped = itertools.izip(*[stripped]*1)

    # takes text file data, saves all to csv
    with open('data_extracted.csv', 'w') as out_file:
       writer = csv.writer(out_file)
       for group in grouped:
           writer.writerows(group)

# reads in the csv of all data extracted from text file
data = pd.read_csv("data_extracted.csv")

# Select only the rows we need
data = data[:3]

# output new csv file with columns 0 and 29
csvfile = "Pahala_SO2.csv"
outdf = pd.DataFrame()
outdf['Date Time'] = data.iloc[:,0]
outdf['Ensemble Mean'] = data.iloc[:,29]

outdf.to_csv(csvfile, index=False)

"""
Sample of first 4 rows of txt file: 
2018051012 1525953600 0.022 0.016 0 0 0 4.39e-05 0.029 0.00656 0.00412 0 0 0         
0 0 0 0 0 0 0.00103 0.036 0.018 8.93e-05 0.00117 0 0.000208 0.014 0.00411 
0.022
2018051013 1525957200 0 0 0 0 0 0.00023 7.26e-05 0.045 0 0 0 0 0 0 0 0 0 0 0 
0.00863 0 0 0.00106 0 0 0 0 0
2018051014 1525960800 0 0 0 0 0 0 0.028 0.011 0.039 0 0 0 0 0 0 0 0 0 
0.00024 0.017 0.000342 0.000306 0.000637 0.000945 0.000249 0.126 0.000343 0
2018051015 1525964400 0.000174 7.8e-05 0.000418 0.03 8.68e-05 0 0.00106 
0.175 0 0 0 0 0 0 0 0 0 0 0 0 0.014 0.000498 0.000209 0.00235 0 0.000234 0 
0.000174
2018051016 1525968000 0.00046 0.037 0.000759 0.000168 0.00121 0.015 0.00836 
0.027 0.000236 0 0 0 0 0 0 0 0 0 0.08 0.025 0.112 0.092 0.099 0.122 0.034 
0.169 0.108 0.00046
"""

非常感谢任何帮助,谢谢!

0 个答案:

没有答案