简体   繁体   English

如何在python中分割线

[英]How to split lines in python

I am looking for a simple way to split lines in python from a .txt file and then just read out the names and compare them to another file.我正在寻找一种简单的方法来从 .txt 文件中拆分 python 中的行,然后读出名称并将它们与另一个文件进行比较。

I've had a code that split the lines successfully, but I couldn't find a way to read out just the names, unfortunately the code that split it successfully was lost.我有一个成功分割行的代码,但我找不到一种方法来读出名称,不幸的是,成功分割它的代码丢失了。

this is what the .txt file looks like.这就是 .txt 文件的样子。

Id;Name;Job;
1;James;IT;
2;Adam;Director;
3;Clare;Assisiant;

example if the code I currently have (doesn't output anything)例如,如果我当前拥有的代码(不输出任何内容)

my_file = open("HP_liki.txt","r")
flag = index = 0 
x1=""
for line in my_file: 
    line.strip().split('\n') 
    index+=1 
content = my_file.read()
list=[]
lines_to_read = [index-1]
for position, line1 in enumerate(x1):
    if position in lines_to_read:
        list=line1
        x1=list.split(";")
    print(x1[1])

I need a solution that doesn't import pandas or csv .我需要一个不导入pandascsv的解决方案。

The first part of your code confuses me as to your purpose.你的代码的第一部分让我对你的目的感到困惑。

for line in my_file: 
    line.strip().split('\n') 
    index+=1 
content = my_file.read()

Your for loop iterates through the file and strips each line.您的for循环遍历文件并删除每一行。 Then it splits on a newline, which cannot exist at this point.然后它在换行符上拆分,此时不存在 The for already iterates by lines, so there is no newline in any line in this loop. for已经按行迭代,所以在这个循环中的任何line中都没有换行符。

In addition, once you've stripped the line, you ignore the result, increment index , and leave the loop.此外,一旦你去掉了这条线,你就会忽略结果,增加index ,并离开循环。 As a result, all this loop accomplishes is to count the lines in the file.因此,这个循环完成的所有工作就是计算文件中的行数。

The line after the loop reads from a file that has no more data, so it will simply handle the EOF exception and return nothing.循环之后的行从没有更多数据的文件中读取,因此它将简单地处理 EOF 异常并且不返回任何内容。


If you want the names from the file, then use the built-in file read to iterate through the file, split each line, and extract the second field:如果您想要文件中的名称,则使用内置文件读取来遍历文件,拆分每一行,然后提取第二个字段:

name_list = [line.split(';')[1]
               for line in open("HP_liki.txt","r") ]

name_list also includes the header "Name" , which you can easily delete. name_list还包括标题"Name" ,您可以轻松删除它。

Does that handle your problem?这能解决你的问题吗?

I have my dataf.txt file:我有我的 dataf.txt 文件:

Id;Name;Job;
1;James;IT;
2;Adam;Director;
3;Clare;Assisiant;

I have written this to extract information:我写这个是为了提取信息:

with open('dataf.txt','r') as fl:
    data = fl.readlines()
    a = [i.replace('\n','').split(';')[:-1] for i in data]
    print(a[1:])

Outputs:输出:

[['1', 'James', 'IT'], ['2', 'Adam', 'Director'], ['3', 'Clare', 'Assisiant']]

So without using any external library you can use simple file io and then generalize according to your need.因此,在不使用任何外部库的情况下,您可以使用简单的文件 io,然后根据您的需要进行概括。

readfile.py读取文件

file = open('datafile.txt','r')

for line in file:
    line_split = line.split(';')
    if (line_split[0].isdigit()):
        print(line_split[1])

file.close()

datafile.txt数据文件.txt

Id;Name;Job;
1;James;IT;
2;Adam;Director;
3;Clare;Assisiant;

If you run this you'll have output如果你运行这个你会有输出

James
Adam
Clare

You can change the if condition according to your need您可以根据需要更改if条件

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

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