0

私はPythonを初めて使用します。私が欲しいのは、このようなファイルのコンテンツを印刷できるようにすることです。

マッシュポテト、これとあれをトッピング...................... 9.99 $

同様に

Product_name、description.........................価格

Product_namesのみを含むファイルと照合した場合

マッシュポテト

過去

シーザーサラダ

などなど。

最初のファイルの内容は均一な順序ではないので、検索、照合、印刷のアプローチで試しています。

私の問題を理解していただければ幸いです

これは私が試したことです

     import re

      content_file = open('/Users/ashishyadav/Downloads/pdfminer-20110515/samples/te.txt',"r")
      product_list = open('/Users/ashishyadav/Desktop/AQ/te.txt',"r")
      output = open("output.txt" , "w")
      line = content_file.read().lower().strip()
      for prod in product_list:
        for match in re.finditer(prod.lower().strip(), line):
         s=match.start()
         e=match.end()
         print >>output, match.group(),"\t",
         print >>output, '%d:%d' % ( s, e),"\n",

私のコードは、2番目の製品リストファイルを完全なコンテンツファイルと一致させますが、説明と価格ではなく、product_Namesのインデックスのみを提供します。

私が欲しいのは、Product_nameからpriceまでのインデックス/スパンです。

マッシュポテトのように----9.99$(マッシュポテト-[0:58]),,mちょうど[0:14]

また、同じアプローチを使用して説明と価格を印刷する方法

前もって感謝します

4

1 に答える 1

1
  • 「2 番目のファイル」全体をセット X に読み込みます。
  • 「最初の」ファイルを 1 行ずつ読み取ります。
  • 各行について、カンマの前の部分を抽出します。
  • このパーツがセット X に含まれている場合は、必要に応じて印刷します。

Pythonでこれが必要な場合はお知らせください。

# Read the whole "second file" into a set X.
with open('foo') as fp:
    names = set(fp)

# Read the "first" file line by line.
with open('bar') as fp:
    for line in fp:

        # For each line, extract the part before the comma.
        name = line.split(',')[0]

        # If this part is in the set X, print whatever is desired.
        if name in names:
             print line
于 2012-05-18T09:05:59.517 に答える