简体   繁体   English

在Python中单行打印

[英]Print in single line in Python

Given some code: 给出一些代码:

keyword=re.findall(r'ke\w+ = \S+',s)
score=re.findall(r'sc\w+ = \S+',s)
print '%s,%s' %(keyword,score)

The output of above code is: 上面的代码输出为:

['keyword = NORTH', 'keyword = GUESS', 'keyword = DRESSES', 'keyword = RALPH', 'keyword = MATERIAL'],['score = 88466', 'score = 83965', 'score = 79379', 'score = 74897', 'score = 68168'] ['关键字= NORTH','关键字= GUESS','关键字= DRESSES','关键字= RALPH','关键字=材料'],['得分= 88466','得分= 83965','得分= 79379' ,“得分= 74897”,“得分= 68168”]

But I want the format should be different lines like: 但我希望格式应为不同的行,例如:

NORTH,88466
GUESS,83935
DRESSES,83935
RALPH,73379
MATERIAL,68168

Instead of the last line, do this instead: 代替最后一行,而是这样做:

>>> for k, s in zip(keyword, score):
        kw = k.partition('=')[2].strip()
        sc = s.partition('=')[2].strip()
        print '%s,%s' % (kw, sc)


NORTH,88466
GUESS,83965
DRESSES,79379
RALPH,74897
MATERIAL,68168

Here is how it works: 下面是它的工作原理:

  • The zip brings the corresponding elements together pairwise. 拉链将相应的元素成对放置。

  • The partition splits a string like 'keyword = NORTH' into three parts (the part before the equal sign, the equal sign itself, and the part after. The [2] keeps only the latter part. 分区'keyword = NORTH'类的字符串分为三部分(等号之前的部分,等号本身和之后的部分。 [2]仅保留后一部分。

  • The strip removes leading and trailing whitespace. 该条带删除前导和尾随空格。

Alternatively, you can modify your regexes to do much of the work for you by using groups to capture the keywords and scores without the surrounding text: 另外,您可以通过使用组来捕获关键字和分数而不用周围的文本来修改正则表达式来为您完成很多工作:

keywords = re.findall(r'ke\w+ = (\S+)',s)
scores = re.findall(r'sc\w+ = (\S+)',s)
for keyword, score in zip(keywords, scores):
    print '%s,%s' %(keyword,score)

One way would be like would be to zip() the two lists together (to iterate over them pairwise) and use str.partition() to grab the data after the = , like this:: 一种方法是将两个列表一起zip()在一起(以成对方式对其进行迭代),然后使用str.partition()来获取=之后的数据,如下所示:

def after_equals(s):
    return s.partition(' = ')[-1]

for k,s in zip(keyword, score):
    print after_equals(k) + ',' + after_equals(s)

If you don't want to call after_equals() twice, you could refactor to: 如果您不想两次调用after_equals() ,则可以重构为:

for pair in zip(keyword, score):
    print ','.join(after_equals(data) for data in pair)

If you want to write to a text file (you really should have mentioned this in the question, not in your comments on my answer), then you can take this approach... 如果您要写入文本文件(您确实应该在问题中提到此内容,而不是在对我的答案的评论中提到),那么可以采用这种方法...

with open('output.txt', 'w+') as output:
    for pair in zip(keyword, score):
        output.write(','.join(after_equals(data) for data in pair) + '\n')

Output: 输出:

% cat output.txt
NORTH,88466
GUESS,83965
DRESSES,79379
RALPH,74897
MATERIAL,68168

Hope this will help: 希望这会有所帮助:

keyword = ['NORTH','GUESS','DERESSES','RALPH']
score = [88466,83935,83935,73379]

for key,value in zip(keyword,score):
  print "%s,%s" %(key,value)

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

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