简体   繁体   English

在单行上打印不起作用Python 3

[英]Print on a single line not working Python 3

I'm very new to programming and am working on some code to extract data from a bunch of text files. 我是编程新手,正在研究一些代码以从一堆文本文件中提取数据。 I've been able to do this however the data is not useful to me in Excel. 我已经能够做到这一点,但是数据对我在Excel中没有用。 Therefore, I would like to print it all on a single line and separate it by a special character, which I can then delimit in Excel. 因此,我想将它们全部打印在一行上,并用一个特殊字符将其分开,然后可以在Excel中定界。

Here is my code: 这是我的代码:

import os
data=['Find me','find you', 'find us']
with open('C:\\Users\\Documents\\File.txt', 'r') as inF:
    for line in inF:

        for a in data:
            string=a

            if string in line:

                print (line,end='*') #print on same line

    inF.close()

So basically what I'm doing is finding if a keyword is on that line and then printing that line if it is. 因此,基本上我正在做的是查找关键字是否在该行上,然后在该行上打印该行。

Even though I have print(,end='*'), I don't get the print on a single line. 即使我有print(,end ='*'),也没有一行打印出来。 It outputs: 它输出:

Find me

*find you

*find us

Where is the problem? 问题出在哪儿? (I'm using Python 3.5.1) (我正在使用Python 3.5.1)

Your immediate problem is that you're not removing the newline characters from your lines before printing them. 您的直接问题是,在打印行之前没有从行中删除换行符。 The usual way to do this is with strip() , eg: 通常的方法是使用strip() ,例如:

print(line.strip(), end='*')

You'll also print multiple copies of the line if more than one of your special phrases appear in the line. 如果一行中出现多个特殊短语,您还将打印该行的多个副本。 To avoid that, add a break statement after your print, or (better, but a more advanced construct that might not make sense until you're used to generator expressions) use if any(keyword in line for keyword in data): 为了避免这种情况,请在打印后添加一个break语句,或者使用(如果更好(一个更高级的构造,除非您习惯于生成表达式), if any(keyword in line for keyword in data):才有意义)使用if any(keyword in line for keyword in data):

You also don't need to explicitly close the input file - the point of the with open(...) as ...: context manager is that it closes the file when exiting it. 您也不需要显式关闭输入文件with open(...) as ...:的要点是with open(...) as ...:上下文管理器是它在退出文件时将其关闭。

And I would avoid using string as a variable name - it doesn't tell anyone anything about what the variable is used for, and it can cause confusion if you end up using the built-in string module for anything. 而且我会避免将string用作变量名-它不会告诉任何人该变量的用途,并且如果最终对任何内容都使用内置的string模块,可能会引起混乱。 It's not as bad as shadowing a built-in constructor like list , but it's worth avoiding. 它不如隐藏诸如list类的内置构造函数那样糟糕,但是值得避免。 Especially since it does nothing for you here, you can just use if a in line: here if you don't want to use the any() version above. 特别是因为它在这里对您没有任何帮助,因此您可以仅使用if a in line:如果您不想使用上述的any()版本,请在此处使用。

In addition to all that, if your data is not extremely large (and I hope it's not if you're trying to fit it all on one line) you'll get tidier code and avoid the trailing delimiter by using the .join() method on strings, eg something like: 除此之外,如果您的数据不是非常大(并且希望不是全部都放在一行上),您将得到整齐的代码,并通过使用.join()来避免尾随定界符字符串的方法,例如:

import os
data=['Find me','find you', 'find us']
with open('C:\\Users\\Documents\\File.txt', 'r') as inF:
    print "*".join(line.strip() for line in inF if any(keyword in line for keyword in data))

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

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