【发布时间】: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 Ask、help center。
-
@AMC...是的..我尝试在文件中搜索数据 block_ABC_top 并提取该行...但是我如何专门从该行的 Block id 列中提取数据?跨度>
-
请分享您目前的代码,以及您遇到的具体问题。
-
@AMC..edited with code
标签: python csv file text multiple-columns