Insert text into its proper cells - Python

Asked

Viewed 104 times

1

Hello, my friends

I am developing the code so that I can clean and organize the data (as shown above) and one of the best ways was to insert the data into a file CSV so that later I could return it in spreadsheet format, so I applied the following code:

import csv

with open('/content/Pasta2.csv', 'w') as out_file:
      writer = csv.writer(out_file, delimiter=',',lineterminator='\n')
      writer.writerow(('URL','Documento', 'Código CVM', 'Data de Referência', 'Status'))
      writer.writerow([linha_1])

teste_02 = pd.read_csv('/content/Pasta2.csv')

teste_02

However, the return that I have is shown in the image below, the code simply takes the text that is inside the "line_1' and does not divide the cells, I would like to know which code I can implement to solve this problem, can help me?

inserir a descrição da imagem aqui

Expected Result:

inserir a descrição da imagem aqui

2 answers

1

Code that worked:

import csv

with open('/content/Pasta2.csv', 'w') as out_file:
      
      writer = csv.writer(out_file, delimiter=',',lineterminator='')
      writer.writerow(('URL','Documento', 'Código CVM', 'Data de Referência', 'Status'))
      writer.writerow(linha_1.replace('"','').split(','))

teste_02 = pd.read_csv('/content/Pasta2.csv')

teste_02

I made some changes and the result worked, the correct distribution was performed on the lines and also the line breaks.

0

It seems to me that you are writing the entire line in the first cell, trying to exchange the line 6 for:

writer.writerow(*[linha_1])
  • hello, Onilol I tried the way you said, but it didn’t work, the closest I could get was this way, it separated in the columns, but doesn’t break the lines: '' import csv with open('/content/Pasta2.csv', 'w') as out_file: Writer = csv.Writer(out_file, delimiter=',',lineterminator=' n') Writer.writerow(('URL','Document', 'CVM Code', 'Reference Date', 'Status')) Writer.writerow(linha_1.replace("',''').split(' ')) teste_02 = read._csv('/content/Pasta2.csv') teste_02

  • Hmmm, take a look at module documentation csv then I remember that it has a way of mapping the headers and already printa everything cute.

  • 1

    Oops, got it! I’ll post the answer below, thanks for the help, Onilol.

Browser other questions tagged

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