读取文件中的行,如果包含字符串则打印行 [英] Read line in file, print line if it contains string

查看:70
本文介绍了读取文件中的行,如果包含字符串则打印行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个工作代码,它打开一个文件,查找一个字符串,如果它包含该字符串,则打印该行.我这样做是为了我可以手动决定是否应该从我的数据集中删除该行.

I have a working code that opens a file, looks for a string, and prints the line if it contains that string. I'm doing this so that I can decide, manually, whether the line should be removed from my dataset or not.

但是如果我可以告诉程序读取包含两个逗号之间的字符串的那部分行,那就更好了.

But it would be much better if I can tell the program to read the part of the line that contains the string that is between two commas.

我现在拥有的代码(见下文)

The code I have now (see below)

with open("dvd.txt") as f:
    for num, line in enumerate(f, 1):
        if " arnold " in line:
            num = str(num)
            print line + '' + num

像这样打印每一行:

77.224998664,2014-10-19,386.5889,the best arnold ***** ,81,dvd-action,Cheese 5gr,online-dvd-king93,0.19976,18,/media/removable/backup/2014-10-19/all_items/cheese-5gr?feedback_page=1.html,    ships from: Germany    ships to: Worldwide  ,2014-07-30,online-dvd-king,93 1

我希望它改为打印:

,the best arnold ***** , 1

the best arnold *****  1

我阅读了this 问题,但我希望避免使用 CSV.

I read this question, but I hope to avoid using CSV.

如果由于某种原因很难找到逗号或任何其他特定字符之间的文本,那么打印我要查找的字符串前后的 3 个单词会很有用.

If it is for whatever reason tricky to find the text between commas, or any other specific characters, it'd be useful to print the 3 words before and after the string I'm looking for.

推荐答案

这很简单,str.split().如下修改您的函数将产生您想要的输出.

This is very simple to do with str.split(). Modifying your function as follows will produce the output you want.

with open("dvd.csv") as f:
    for num, line in enumerate(f, 1):
        if " arnold " in line:
            num = str(num)
            print line.split(',')[3] + '' + num 

str.split 按指定的分隔符将字符串拆分为列表.要访问您想要的列表条目,只需提供适当的索引(在您的情况下应该是 3).

str.split splits up a string into a list by the specified separator. To access the list entry you want, simply supply the appropriate index (which in your case should be 3).

顺便说一句,您可以使用 str.format() 方法让它更好一点:

As an aside, you can produce your output with the str.format() method to make it a little nicer:

print "{} {}".format(line.split(',')[3], num)

这也将允许您删除 num = str(num) 因为 format 方法可以处理多种数据类型(与不能处理的字符串连接相反).

This will also allow you to remove num = str(num) since the format method can handle multiple datatypes (as opposed to string concatenation which cannot).

这篇关于读取文件中的行,如果包含字符串则打印行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆