Python Forum
Can't import csv data - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Can't import csv data (/thread-38210.html)



Can't import csv data - JonWayn - Sep-16-2022

in the code below, page is the webpage response from Playwright for what it's worth:
The csv file gets created ok and looks good as far as I can tell. But then when I go to import this file into a Microsoft Access database, nothing is imported. Is there some sort of conversion that needs to be done before writing the data to the csv file?
Thank you

                for row in page.query_selector_all("table tbody tr"):
                    i += 1
                    link = row.query_selector("xpath=td/a[text()='View']")
                    url = link.get_attribute('href')
                    charges = process_details(url)
                    if charges not in ['', 'err']:
                        charges = fix_charge(charges)
                        print(charges)
                        for charge in charges.split(';'):
                            print('------', charge)
                            record = [
                                    row.query_selector_all("td")[1].inner_text(),
                                    row.query_selector_all("td")[2].inner_text(),
                                    row.query_selector_all("td")[3].inner_text(),
                                    row.query_selector_all("td")[4].inner_text(),
                                    row.query_selector_all("td")[6].inner_text(),
                                    charge.strip().replace(',', ';'),
                                    new_date
                            ]
                            if charge.strip() not in ['', ' ']:
                                output_file.writerow(record)
                page.go_back()
        file.close()


def fix_charge(charge):
    charge = charge[charge.index(' '):].lstrip()

    for num in re.findall('\d+', charge):
        charge = charge.replace(num, '1')
    charge = charge.replace('1.', ';')
    charge = charge.replace('1-', ';')
    charge = charge.replace('1)', ';')
    charge = charge.replace('(1)', ';')
    charge = charge.replace(' 1 ', ';')
    charge = charge.strip().strip(',')
    return charge.strip()



RE: Can't import csv data - buran - Sep-16-2022

(Sep-16-2022, 10:43 AM)JonWayn Wrote: The csv file gets created ok and looks good as far as I can tell.
Then why do you think there is problem with the created file? It looks there is problem with the import of csv file into Microsoft Access. This is the wrong forum to ask question about Microsoft Access.
That said, without sample csv file even if someone wants to test import into Microsoft Access, they cannot do so.


RE: Can't import csv data - rob101 - Sep-16-2022

(Sep-16-2022, 10:43 AM)JonWayn Wrote: But then when I go to import this file into a Microsoft Access database, nothing is imported.

It's been a number of years since I worked with MS Access, but from what I can recall: make sure that the separator is set to comma, and not some other separator. Also, I think I remember a setting that tells Access if the first row contains the header information. If not, the table will be created with default header names, which you can, of course, edit, post import.


RE: Can't import csv data - JonWayn - Sep-18-2022

(Sep-16-2022, 11:01 AM)buran Wrote:
(Sep-16-2022, 10:43 AM)JonWayn Wrote: The csv file gets created ok and looks good as far as I can tell.
Then why do you think there is problem with the created file? It looks there is problem with the import of csv file into Microsoft Access. This is the wrong forum to ask question about Microsoft Access.
That said, without sample csv file even if someone wants to test import into Microsoft Access, they cannot do so.

I just wanted to clear out of the way and conversion issue


RE: Can't import csv data - JonWayn - Sep-18-2022

(Sep-16-2022, 09:28 PM)rob101 Wrote:
(Sep-16-2022, 10:43 AM)JonWayn Wrote: But then when I go to import this file into a Microsoft Access database, nothing is imported.

It's been a number of years since I worked with MS Access, but from what I can recall: make sure that the separator is set to comma, and not some other separator. Also, I think I remember a setting that tells Access if the first row contains the header information. If not, the table will be created with default header names, which you can, of course, edit, post import.

As I found out later, the same file imports ok in another table. So I deleted and recreated the failed table, thinking there might have been some corruption to it. I also created a linked table to the file, using a query to get the records into the table. Still empty. So one table is behaving differently from the other. So it is starting to look like a problem in Access and not the file contents. And yes, Access has the feature to specify that first row is headers