简体   繁体   English

我有一些文件格式的数据。 我想知道每一行的第一个字符串是什么,并计算它们重复了多少次

[英]I have some data in a file format. I want to know what is the first string in each line and count how many times they are repeated

24 

Lattice = " 4.86063327 0.0 0.0 0.0 5.88550146 0.0 0.0 0.0 9.91419763 "

Fe 2.191577 4.414126 7.674144

Fe 0.238740 4.414126 2.717045

Fe 2.669056 1.471375 2.240053

Fe 4.621894 1.471375 7.197152

P 4.381224 4.414126 5.887209

P 2.909726 4.414126 0.930110

P 0.479409 1.471375 4.026988

P 1.950908 1.471375 8.984087

O 1.026405 4.414126 6.149975

O 1.403911 4.414126 1.192876

O 3.834228 1.471375 3.764223

O 3.456722 1.471375 8.721321

O 3.174416 4.414126 9.346927

O 4.116534 4.414126 4.389828

O 1.686217 1.471375 0.567271

O 0.744100 1.471375 5.524369

O 3.659022 5.613562 6.613841

O 3.631928 3.214690 1.656742

O 1.201612 2.670811 3.300357

O 1.228705 0.271940 8.257456

O 1.201612 0.271940 3.300357

O 1.228705 2.670811 8.257456

O 3.659022 3.214690 6.613841

O 3.631928 5.613562 1.656742

I am trying to write a code in Python. However, I am not sure how to make a loop from line 3 to the end.我正在尝试在 Python 中编写代码。但是,我不确定如何从第 3 行到最后进行循环。

I am trying with a code like this我正在尝试使用这样的代码

with open("Structure") as f:
   lines_after_2 = f.readlines()[2:]
for i in range((len(lines_after_2))):
    symbol=lines_after_2[i].split()[0]
    print(symbol)
    if symbol='Fe':
        count_Fe+=1
    elif symbol=='P':
        count_P+=1
    elif symbol=='O':
        count_O+=1
        
        print(count_Fe)
        print(count_P)
        print(count_O)

But it says NameError: name 'count_Fe+=1' is not defined.但它说 NameError: name 'count_Fe+=1' is not defined。

lkhlknlkn nklnkjhn nbkjkjhkjhgjhgchghgchgd jhfhgdfgd jhjhgfkfdytd jjhfdtd. lkhlknlkn nklnkjhn nbkjkjhkjhgjhgchghgchgd jhfhgdfgd jhjhgfkfdytd jjhfdtd。 kjbkjbjhjhvbjh kbjhjhv jbjhgffdytdtrsr jhvhfgfgdsdrts hghfcdrt. kjbkjbjhjhvbjh kbjhjhv jbjhgffdytdtrsr jhvhfgfgdsdrts hghfcdrt。

One option is to use enumerate and specify a condition where the line index is >= 2:一种选择是使用枚举并指定行索引 >= 2 的条件:

with open("sample.txt") as data:
    for idx, line in enumerate(data):
        if idx >= 2:
            print(line)

Another option is to consume the TextIOWrapper with a list comprehension and then slice the list:另一种选择是使用具有列表理解的 TextIOWrapper,然后对列表进行切片:

with open("sample.txt") as data:
    my_lines = [line for line in data][2:]
for line in my_lines:
    print(line)

In a file like this you can just use the count method for strings:在这样的文件中,您可以只对字符串使用 count 方法:

arq = open("Structure")
string = arq.read()
arq.close()

count_Fe = string.count('Fe')

Just take care, if there is something like 'Ferr' it will count the 'Fe'.请注意,如果有类似“Ferr”的内容,它将计算“Fe”。 But in a type of data like your exemple, it should work但是在像你的例子这样的数据类型中,它应该有效

暂无
暂无

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

相关问题 我想要这个 function 在新行“\n”上创建每个新记录的记事本文件,我尝试了很多次但徒劳无功 - I want this function to create notepad file with each new record on new line "\n" i have tried many times but vain exe 文件的输出为 csv 格式。 我想要一个标题和所有数据一起 - Output of an exe file is in csv format. I want one header and all the data together 我的程序没有计算我想要多少欧元的门票 - My program doesn't count of how many tickets you have with some euros as I want 我想知道每个客户下了多少个订单 - I want to know how many orders are placed by each of the customer 我的函数将字符串作为参数,并以其他格式打印。 我想打开一个文件并在文件中的行上调用我的函数 - My function takes a string as an argument and prints in a different format. I want to open a file and call my function on the lines in the file 我有一个json格式的数据文件。 如何找到并打印前 20 个 eij_max 值和相关的 Pretty_formula? 我正在使用蟒蛇 - I have a data file in json format. How can I find and print the top 20 eij_max values and the associated pretty_formula? I am using python 我有一个文本文件。 我想要像这种格式或带有标点符号的搜索短语,或者像混乱的单词格式。 我使用 Python 3.x。 任何 python 库 - I have a text file. I want search phrases like this format or with punctuation or like in jumbled word format. I use Python 3.x. Any python library 我有一个大文件,其中每一行都是json格式,我只想从那几行中提取少量属性。我该怎么做? - I have big file in which each line is in json format , I want to extract only few attributes from that lines..how can i do that.? 我正在尝试使用 Pandas 将 json 文件转为特定格式。 我想在某些列上旋转它 - I am trying to pivot a json file using pandas to be in a specific format. I want to pivot it on certain columns 我有一个包含许多.tar.gz文件的文件夹。 在python中,我想进入每个文件解压缩或压缩,并找到具有要提取的字符串的文本文件? - I have a folder with many .tar.gz files. In python I want to go into each file unzip or compress and find text file that has string I want to extract?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM