【问题标题】:extracting data from columns in a text file using python使用python从文本文件中的列中提取数据
【发布时间】:2020-12-17 06:50:17
【问题描述】:

我是 python 文件数据处理的新手。我有以下文本文件,其中包含新大学校园的报告。我想从“colleges”列和“book_IDs_1”中提取block_ABC_top 23 的数据。我还想知道在colleges 列中是否出现block_ABC_top 并找到book IDs_1 列的值。 可以在文本文件中吗?还是我必须将其更改为 csv?我如何为此数据处理编写代码?请帮帮我!!

Copyright 1986-2019, Inc. All Rights Reserved.

Design Information
-----------------------------------------------------------------------------------------------------------------
| Version : (lin64) Build 2729669 Thu Dec  5 04:48:12 MST 2019
| Date         : Wed Aug 26 00:46:08 2020
| Host         : running 64-bit Red Hat Enterprise Linux Server release 7.8 
| Command      : college report
| Design       : college
| Device       : laptop
| Design State : in construction
-----------------------------------------------------------------------------------------------------------------

Table of Contents
-----------------
1. Information by Hierarchy

1. Information by Hierarchy
---------------------------
+----------------------------------------------+--------------------------------------------+------------+------------+---------+------+-----+
|                   colleges                   |                   Module                   | Total mems | book IDs_1 | canteen | BUS  | UPS | 
+----------------------------------------------+--------------------------------------------+------------+------------+---------+------+-----+
| block_ABC_top                                |                                      (top) |         44 |         23 |       8 |    8 |   8 |   
|    (block_ABC_top_0)                         |                            block_ABC_top_0 |          5 |          5 |       5 |    2 |   9 |       
+----------------------------------------------+--------------------------------------------+------------+------------+---------+------+-----+

我有一个数据列表,其中包含大学的数据,例如 block_ABC_top、block_ABC_top_1、block_ABC_top、block_ABC_top_1...这是我下面的代码 我面临的问题是..它只需要 data[0] 的数据..但是我有 data[0] 和 data[2] 有同一所大学,我希望检查会发生两次。

with open ("utility.txt", 'r') as f1:
            
            for line in f1:
                if data[x] in line:
                    line_values = line.split('|') 

                    if (int(line_values[4]) == 23 or int(line_values[7]) == 8):
                        filecheck = fullpath + "/" + filenames[x]
                        print filecheck

                        #print "check file "+ filenames[x]
                    x = x + 1

            f1.close()

【问题讨论】:

  • 有具体问题吗?你有没有尝试过,做过任何研究?请参阅How to Askhelp center
  • @AMC...是的..我尝试在文件中搜索数据 block_ABC_top 并提取该行...但是我如何专门从该行的 Block id 列中提取数据?跨度>
  • 请分享您目前的代码,以及您遇到的具体问题。
  • @AMC..edited with code

标签: python csv file text multiple-columns


【解决方案1】:
print [x.split(' ')[0] for x in open(file).readlines()]  #colleges column
print [x.split(' ')[3] for x in open(file).readlines()]  #book_IDs_1 column

尝试运行这些。

【讨论】:

  • @yansh...我运行了这个...但我特别想要来自书籍 IDs_1 列的 block_ABC_top 的数据...不单独提取数据..输入文件也有一些关于文本的详细信息榜首...:(
【解决方案2】:

与其使用到达字段的确切位置,更好的方法是使用split() 函数,因为您的字段由| 符号分隔。您可以遍历文件的行并相应地处理它们。

for loop...:
    line_values = line.split("|")

print(line_values[0]) # block_ABC_top

【讨论】:

    【解决方案3】:

    要提取 Book id 列数据,请使用下面的代码

    with open('report.txt') as f:
      for line in f:
        if 'block_ABC_top' in line:
          line_values = line.split('|')
          print(line_values[4]) # PRINTS 23 AND 5
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-13
      • 1970-01-01
      • 1970-01-01
      • 2018-06-14
      • 1970-01-01
      • 1970-01-01
      • 2021-05-22
      • 1970-01-01
      相关资源
      最近更新 更多