CSV import removes all spaces

I am trying to import CSV, everything is ok but it removes all spaces, how do I prevent this ?

CSV:

First_Name,Last_Name,Date_of_Birth
Tom,Cruise,"July 3, 1962"
Bruce,Willis,"March 19, 1955"
Morgan,Freeman,"June 1, 1937"
John,Wayne,"May 26, 1907"

SCRIPT:

@anvil.server.callable
def read_csv(csv_string):
 
    csv_list=csv_string.split()
    reader=csv.reader(csv_list, delimiter=',', quotechar='"')
            
    i = reader.next()
    print 'headers', i
    
    data = [r for r in reader] 
    data.pop(0) # remove header
             
    for line in data:

        app_tables.textile.add_row(
          sku=line[0],
          name=line[1],
          price=line[2]
        )

OUTPUT:

headers:  ['First_Name', 'Last_Name', 'Date_of_Birth']

data: [['Bruce', 'Willis', 'March19,1955'], ['Morgan', 'Freeman', 'June1,1937'], ['John', 'Wayne', 'May26,1907'], ['John', 'Wayne', 'May261907']]

Hi - that output is not from that code.

edit it’s ok, I reproduced the error here. Looking …

https://anvil.works/build#clone:K7JK7VN3PGTY2EXM=7ZNM62SM274UJS34Z4DGJH3L

edit: OK, you were faster :slight_smile:

Ok, this works for me. It also works if I pass that string as a parameter from the client.

import anvil.server
import csv

@anvil.server.callable
def read_csv():
    csv_string = '"First_Name","Last_Name","Date_of_Birth"\nTom,Cruise,"July 3, 1962"\nBruce,Willis,"March 19, 1955"\nMorgan,Freeman,"June 1, 1937"\nJohn,Wayne,"May 26, 1907"\n'
    
    csv_list = csv_string.split("\n")
    reader=csv.reader(csv_list, delimiter=',')

    print("headers",reader.next())
    for r in reader:
      print(r)

My output is :
('headers', ['First_Name', 'Last_Name', 'Date_of_Birth']) ['Tom', 'Cruise', 'July 3, 1962'] ['Bruce', 'Willis', 'March 19, 1955'] ['Morgan', 'Freeman', 'June 1, 1937'] ['John', 'Wayne', 'May 26, 1907'] []

it works well with string, but I can not get it work uploading from CSV file.

Is it possible that I am doing something wrong in the first part when I am uploading the file ?

  def file_loader_1_change(self, file, **event_args):
 
    for f in self.file_loader_1.files:
      
     print 'filename', f.name
     filedata=f.get_bytes()
     anvil.server.call('read_csv',filedata)

The CSV file that I need to import is very large, this is just sample.

Note this code is for python 2.
In python 3 there appears to be no “next” method (I’ve not looked for an alternative).

This works :

  # Client code.
  def file_loader_1_change(self, file, **event_args):
    for f in self.file_loader_1.files:
      print 'filename', f.name
      filedata=f.get_bytes()      
      anvil.server.call('read_csv',filedata)
# Server code.
@anvil.server.callable
def read_csv(csv_string):
  csv_text = csv_string.split("\n")
  reader=csv.reader(csv_text, delimiter=',')

  print("headers",reader.next())
  for r in reader:
    print(r)

I used the following text file :

“First_Name”,“Last_Name”,“Date_of_Birth”
Tom,Cruise,“July 3, 1962”
Bruce,Willis,“March 19, 1955”
Morgan,Freeman,“June 1, 1937”
John,Wayne,“May 26, 1907”

and it printed this to the output :

(‘headers’, [‘First_Name’, ‘Last_Name’, ‘Date_of_Birth’])
[‘Tom’, ‘Cruise’, ‘July 3, 1962’]
[‘Bruce’, ‘Willis’, ‘March 19, 1955’]
[‘Morgan’, ‘Freeman’, ‘June 1, 1937’]
[‘John’, ‘Wayne’, ‘May 26, 1907’]
[]

https://anvil.works/build#clone:BMGP2G7ZJ3HUT355=WVBNVXRJOZBQ3KHZPDPF7QJP

1 Like

it works, thanks a lot !

1 Like