Comparing lines from a file with data from a spreadsheet

Asked

Viewed 1,338 times

2

I’m new to Python and I have a problem that’s getting me out of the picture...

Here’s the thing, I want to get the strings of a file and compare its elements with the sentences written in the spreadsheets of my archive .xlsx.

Only that the strings of the file will have what is written in the spreadsheet plus some codes.

I want the program to do the following: If in the file line you have what is in the column I am going through do the instruction else, jump to the next line.

Follows the code:

#!/usr/bin/python
import subprocess
import xlrd

arquivo = open("/home/weslei/Documentos/t.txt", "r")
read = arquivo.readlines()

xls = xlrd.open_workbook('chips.xlsx')
plan = xls.sheets()[0]
b =  plan.col(0)

for i in read:
   for n in b:
       if n == i[20:57]:
           print "instrucao"
       else:
           print "next"

The file I’m reading has this content:

|7891515433963     |AMENDOIM SEM PELE MANIX 40G          |UN|34119/6|AF  4,9900|
|7897846301872     |AREIA HIG ABSORCAT             4KG   |UN|32306/2|AF  7,9900|
|7898948468012     |ARROZ CARRIJO TIPO1            5KG   |UN|32471/7|AF 13,8000|
|7896290300974     |ARROZ PRATO FINO ORGAN INTEG 1K UN   |UN|33908/7|AF 14,9500|
|7896290300318     |ARROZ PRATO FINO PARBOLIZADO   2KG   |UN|32034/4|AF  8,7500|
|7896290300295     |ARROZ PRATO FINO PARBOLIZADO 1K UN   |UN|32185/3|AF  4,3900|
|0000000000000     |ARROZ PRATO RICO AGULINHA 5KG        |UN|34335/0|AF  0,0000|

And the spreadsheet:

ELMA CHIPS  
AMENDOIM SEM PELE MANIX 40G 1,79
BACONZITOS 55G  3,68
*BACONZITOS 110G    6,15
*BATATA SENSAÇÃO FG.GRELH. 90G  6,15
*CEBOLITOS 60G  3,68
*CEBOLITOS ASSADO 110G/120G 6,49
*CHEETOS 51G/ 55G/57G/59G/61G    2,49 
*CHEETOS  130G/150G/160G    6,28
*DEMONTÃO RUFLES BACON. 75G 4,45
*DORITOS 55G    3,65
*DORITOS QUEIJO 96G/110G/100G   6,25
*DORITOS QUEIJO NACHO 167GR 9,98
*DORITOS  200G/220G 9,98
*FANDANGOS 63G   2,45 
*FANDANGOS PRESUNTO/QUEIJO 175G 6,25
*FANDANGOS PRESUNTO 164GR   6,25
*PANETINI PRESUNTO / QUEIJO 40G 1,89
*PINGO D'OURO 65G   2,99
*PINGO D'OURO 90G   3,68
*RUFFLES 90G/100G   6,15
RUFFLES 96GR    6,15
*RUFFLES 175G    9,98 
*RUFFLES 57/50G  3,50 
*SALGADINHOS TORCIDA 60/50G 1,75

Detail: The program outputs the columns as text:u'*SALGADINHOS TORCIDA 60/50G', all columns come out with the text:u, causing the pq iteration error in the file has no text:u.

  • What version of Python? and how are you emitting the output?

  • Python 2.7.12 (default, Jul 1 2016, 15:12:24) [GCC 5.4.0 20160609] on Linux2

1 answer

0


I want the program to do the following: If in the file line you have what is in the column I’m going through: do the instruction else: jump to the next line.

To move to the next iteration you can use the continue. It would not be more practical to check if the column content is present in the row and use the continue?

The program outputs the columns as text:u'*SALGADINHOS TORCIDA 60/50G' all columns come out with the text:u, causing the pq iteration error in the file has no text:u.

You must be using Python 2.x, the prefix u indicates that it is a string Unicode. You can use the function str to return a string or encode to UTF-8 with the function str.encode, for example: nome_da_variavel.encode('utf-8').

Two suggestions:

  • Use the with when opening the file, so the file identifier will be closed automatically after use. It is similar to the using of the C#.
  • Instead of comparing if n == i[20:57], you can do if n in i.

The code might look like this (I tested it in Python 3, it might look a little different in Python 2):

#!/usr/bin/env python
# -*- coding: utf-8 -*-

with open('t.txt', 'r') as arquivo:
    xls = xlrd.open_workbook('chips.xlsx')

    planilha = xls.sheets()[0]
    coluna = planilha.col(0)

    for linha in arquivo:
        linha = linha.rstrip() # Para remover a quebra de linha

        for item in coluna:
            if item.value in linha:
                print ("Item encontrado: ", item)
                # Aqui você pode usar o "continue" para pular para o próximo item
  • and oh man, thanks for the tip I think I’ll follow by in same, the code returned that error

  • File "t. py", line 17, in <module> if item in line: Typeerror: 'in <string>' requires string as left operand, not Cell

  • I will try to convert the "item" to str (because it is the same type as this in the compared file) and send the return here

  • @Safadão Arrange. Try it like this if item.value in linha:. I don’t have much experience with xlrd, but see if it works that way.

  • aae brother worked out item.value, output :

  • ('Item found: ', text:')

  • Rigadão need can call helped dmss vlw

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.