How to check if a certain value of a column is equal and does not save this value in file?

Asked

Viewed 106 times

0

Good afternoon! I’ve been in a jam for days because I can’t seem to make sense of the problem I’m having. I’m making a query to record data in a type str in python. This query goes through the query and where the values of a column are equal it save in a single row and when finished goes to the next item. the problem is that in the lines where the column value repeats it also repeats the write in the file that duplicates the data.

Follow the image where the file is with the duplicated lines and the code I use to generate the file.

cursorVendas = con.cursor()
cursorVendaProdutos = con.cursor()
base = ''

cursorVendas.execute('select * from venda')
for vendas in cursorVendas:


quantidade = cursorVendaProdutos.execute('select desccompleta from venda where nrodocto = ' + str(vendas[0]))

i = 1
for produtos in cursorVendaProdutos:
    print(produtos)

    if (i == quantidade) :
        base = base + produtos[0]
    else:
        base = base + produtos[0] + ',' 
        i += 1

base = base + '\n'
print(base)


arquivo = open("base_import.csv", "w")
arquivo.write(base)
arquivo.close()

Arquivo com dados duplicado

2 answers

0

try to change:

cursorVendas.execute('select * from venda')

for:

cursorVendas.execute('select distinct cupom from venda')
  • If I put in the distinct in select it won’t bring me all the coupons, I need all the coupons as the products are different in each coupon.

0

Creates a function and calls it within its function for products.

for c in range(0,len(valor_coluna)):
    if valor_coluna == valor coluna[c]:
        continue

That or use find. Create a function:

funcao(base, valor_coluna):
   x = base.find(valor_coluna)
   if base == x:
        continue

Browser other questions tagged

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