Same code producing distinct results in Codeenvy and Windows 8.1

Asked

Viewed 78 times

0

I had made the following code in codevy.io and Python installed in codenvy is 3.5.1. I took this code and put to run in windows 8 with python 3.6.1.

The purpose of the code is to clean some CSV files and write them concatenated into a single CSV file.In codenvy is working, but when running the code in windows the exit file gets blank lines between each Row, I want to know how to get around this:

import csv
import sys
import os
import glob

interesting_files = glob.glob("Vendas_artigos*")

with open('SaidaVendasArtigos.csv','wt',encoding='utf-8-sig') as fout:
    for filename in sorted(interesting_files):
        with open(filename,'rt',encoding='utf-8-sig') as fin:
            next(fin)
            reader = csv.reader(fin)
            writer = csv.writer(fout)
            for row in reader:
                if row:
                    row.pop(0)
                writer.writerow(row)

Python 3.5.1 in Codenvy:

row
row
row

Python 3.6.1 on Windows 8:

row

row

row
  • First, start by [tour] the site to understand how it works. In it you will find various materials on how to ask, including on formatting questions and answers. Second, try adding an indentation level to the last line writer.writerow(row), making it written only if the line is not empty.

  • Oh yes, sorry, I will do the tour for now. Well, the level of identation in the last line did not work,tho.

  • The result was the same?

  • Sorry to keep you waiting,.

1 answer

1

I may be wrong, but it may be the moment you use writerow the variable row already have line break, so could use .strip thus:

reader = csv.reader(fin)
writer = csv.writer(fout)
for row in reader:
    if row:
        row.pop(0)
    writer.writerow(row.strip('\r\n'))

In the argument of strip, used \r and \n to remove only line breaks (line feed (LF)) and Carriage Return (CR)

Something that made me curious was the use of row.pop(0), I believe this will always remove the first item of the index, affecting the behavior of for which seems to cause a lot of confusion in the for even, I myself did not understand the need to manipulate the vector if you are just copied the data to a new file, I think maybe you wanted to do something like this:

for row in reader:
    if row:
        writer.writerow(row)

Or:

for row in reader:
    if row:
        writer.writerow(row.strip('\r\n'))

And the reader if you really want to "manipulate" for later use, you could manipulate with pop before or after another for

  • 1

    Well repaired in relation to pop; I had not noticed.

  • So Guilherme, the: for Row in Reader: if Row: Row.pop(0) Writer.writerow(Row.strip(' r n')) generated a blank file. 'Row.pop(0)' is because it needed to delete the first column of each CSV file that I’m opening to write in the output, which concatenates all other "clean" files, you know? What I did that solved,use the light that you focused on the "writerow" method and I saw that I could put the parameter ", newline='' ", which solved my problem.

  • Shit, the comments can only be edited in 5 minutes :X was a mess my answer. Sorry.

Browser other questions tagged

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