Python 使用csv将结果写入.txt文件

Python 使用csv将结果写入.txt文件,python,list,csv,Python,List,Csv,我需要这个脚本的结果,即pygoogle搜索结果,如下所示: name # of results name # of results name # of results 以下是我到目前为止所拥有的,我如何做到这一点而不必每次重新写入文件: import re import pygoogle import csv from pygoogle import pygoogle #creates list with open('parse2.txt') as f: lines

我需要这个脚本的结果,即pygoogle搜索结果,如下所示:

name    # of results
name    # of results
name    # of results
以下是我到目前为止所拥有的,我如何做到这一点而不必每次重新写入文件:

import re
import pygoogle
import csv
from pygoogle import pygoogle
#creates list
with open('parse2.txt') as f:
    lines = [x.strip() for x in f.read().strip('\'"[]').split(' '*6)]
#googles each name in list
for line in lines:
    g = pygoogle(line)
    g.pages = 1
    names = [line + "    " + "%s results" %(g.get_result_count())]
    if (g.get_result_count()) == 0:
        print "ERROR. SEARCH NOT SUCCSESSFUL. TRY AGAIN IN A FEW MINUTES."
    elif (g.get_result_count()) > 0:
    print names
    for name in names:
        with open("output.txt", "wb+") as f:
            f.writelines(name)

当我运行脚本时,输出仅显示最近的一个,因为它正在重新写入脚本:

要写入附加到文件,而不重写,请将
+
添加到模式:

for name in names:
    with open("output.txt", "wb+") as f:
        writer = csv.writer(f)
        writer.writerows(A)
另一方面,为了提高效率,您只能打开文件一次,并使用文件方法代替CSV模块:

with open("output.txt", "wb+") as f:
    f.writelines(A)
大概是这样的:

>>> import csv
>>> A = ["blah blah", "blah blah", "blah", "list"]
>>> lis = [y for x in A for y in x.split()]
>>> lis
['blah', 'blah', 'blah', 'blah', 'blah', 'list']
>>> it = iter(lis)
>>> with open("output.csv", "wb") as f:
         writer = csv.writer(f, delimiter=' ')
         writer.writerows([ [x,next(it)] for x in it])
克服循环行为的混乱:
names
变量将是一个列表,每次使用时其中只有一项。改为这样做:

import re
import csv
from pygoogle import pygoogle

names = []

with open('parse2.txt') as fin:
   names = [x.strip() for x in fin.read().strip('\'"[]').split(' '*6)]

with open("output.txt") as fout:
  for name in names:
    g = pygoogle(name)
    g.pages = 1
    if (g.get_result_count()) == 0:
      print "[Error]: could find no result for '{}'".format(name)
    else:
      fout.write("{}    {} results\n".format(name, g.get_result_count()) )
写一次文件 不覆盖以前的查询

您需要使用和为语句颠倒
的顺序,这将打开文件一次:

with open("output.txt", "wb+") as f:
  for line in lines:
    # Stuff...
    for name in names:
      f.writelines(name)
或者,以附加模式打开文件:

for name in names:
    with open("output.txt", "a") as f:
        f.writelines(name)
在这种情况下,将在末尾添加数据

转换数据 为得到你想要的东西而采取的步骤

  • 将原始列表转换为单词列表
  • 将列表分组
  • 写出这对
  • 详情如下:

    import re
    from itertools import *
    
    A = ["blah blah", "blah blah", "blah", "list"]
    
    #
    # from itertools doc page
    #
    def flatten(listOfLists):
      "Flatten one level of nesting"
      return list(chain.from_iterable(listOfLists))
    
    def pairwise(t):
      it = iter(t)
      return izip(it,it)
    
    #
    # Transform data
    #
    list_of_lists = [re.split("[ ,]", item) for item in A]
    # [['blah', 'blah'], ['blah', 'blah'], ['blah'], ['list']]
    a_words = flatten(list_of_lists)
    a_pairs = pairwise(a_words)
    
    with open("output.csv", "wb") as f:
        writer = csv.writer(f)
        writer.writerows(a_pairs)
    
    更简洁地写为:

    A_pairs = pairwise(flatten([re.split("[ ,]", item) for item in A]))
    with open("output.csv", "wb") as f:
        csv.writer(f).writerows(A_pairs)
    
    以正确的格式写出 如果输出中不需要逗号,只需为
    csvwriter
    定义一种自定义方言即可:

    >>> csv.register_dialect('mydialect', delimiter=' ', quoting=csv.QUOTE_MINIMAL)
    >>> csv.writer(open("try.csv", "w"), dialect="mydialect").writerows(a_ps)
    
    这给了你想要的:

    ➤ cat try.csv 
    blah blah
    blah blah
    blah list
    

    A
    不是列表,而是字符串。你可能想先把它分开。什么是
    name
    ?每次执行
    for name
    循环时,您也在清除和重写“output.csv”。为什么您实际使用
    csv.writer
    ?自己写文件不是更容易吗?类似于“
    ”,“.join(您的_列表)
    ?实际上是由逗号分隔的值组成的字符串吗?添加括号确实使它成为一个列表,但现在它是一个len==1的列表。。。它实际上是一个列表
    a=['blah blah','blah blah','blah list']
    ,还是一个字符串?完全改变这个问题有什么用?这在添加文件时起到了作用,但有没有办法让它像我描述的那样,将每个名称和结果放在一行中?