LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-25-2023, 12:47 PM   #1
piotrbujakowski
LQ Newbie
 
Registered: Jan 2017
Location: London, UK
Distribution: Ubuntu
Posts: 29

Rep: Reputation: 0
Export data base to Excel file


Hi.

I try write code to export data base to Excel file.
Ageing I have a problem with modules.
I try find how install that and still don't works.
Please help, thanks.

# beginning of code

import pandas as pd
import openpyxl

# welcome
print("That is simple data base.")
print("Please input date.")
print('\n')

# input data
username = input("Name: ")
usersurename = input("Surename: ")
age = input("Enter age: ")
height = input("Enter height: ")
yob = input("Enter YoB: ")

# open file
f = open("data_base.txt", "a")

# write date to file
f.write('\n')
f.write("Name: ")
f.write(username)
f.write('\n')
f.write("Surename: ")
f.write(usersurename)
f.write('\n')
f.write("Age: ")
f.write(age)
f.write('\n')
f.write("height: ")
f.write(height)
f.write('\n')
f.write("yob: ")
f.write(yob)
f.write('\n')
f.write('\n')
# close file
f.close()

# open file data base and print data
f = open("data_base.txt", "r")
print(f.read())

# export to excel
df = pd.DataFrame([[username, usersurename, age, height, yob], [12, 22, 32], [31, 32, 33]],
index=['Name', 'Surnename', 'Age', 'height', 'yob'], columns=['a', 'b', 'c'])

print(df)
# end export to excel

# end of code

Python 3.11.0rc1 (main, Aug 12 2022, 10:02:14) [GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license()" for more information.

======== RESTART: /home/piotr/python/data_base/02_data_base_export_to_excel.py ========
Traceback (most recent call last):
File "/usr/lib/python3.11/idlelib/run.py", line 578, in runcode
exec(code, self.locals)
File "/home/piotr/python/data_base/02_data_base_export_to_excel.py", line 3, in <module>
import pandas as pd
File "/usr/lib/python3/dist-packages/pandas/__init__.py", line 22, in <module>
raise ImportError(
ImportError: Unable to import required dependencies:
numpy:

IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!

Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.

We have compiled some common reasons and troubleshooting tips at:

https://numpy.org/devdocs/user/troub...porterror.html

Please note and check the following:

* The Python version is: Python3.11 from "/usr/bin/python3.11"
* The NumPy version is: "1.21.5"

and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.

Original error was: No module named 'numpy.core._multiarray_umath'
 
Old 03-25-2023, 01:09 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,760

Rep: Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983
Quote:
Originally Posted by piotrbujakowski View Post
Hi.
[CODE]I try write code to export data base to Excel file. Ageing I have a problem with modules. I try find how install that and still don't works. Please help, thanks.
Code:
# beginning of code

import pandas as pd
import openpyxl

# welcome
print("That is simple data base.")
print("Please input date.")
print('\n')

# input data
username = input("Name: ")
usersurename = input("Surename: ")
age = input("Enter age: ")
height = input("Enter height: ")
yob = input("Enter YoB: ")

# open file
f = open("data_base.txt", "a")

# write date to file
f.write('\n')
f.write("Name: ")
f.write(username)
f.write('\n')
f.write("Surename: ")
f.write(usersurename)
f.write('\n')
f.write("Age: ")
f.write(age)
f.write('\n')
f.write("height: ")
f.write(height)
f.write('\n')
f.write("yob: ")
f.write(yob)
f.write('\n')
f.write('\n')
# close file
f.close()

# open file data base and print data
f = open("data_base.txt", "r")
print(f.read())

# export to excel
df = pd.DataFrame([[username, usersurename, age, height, yob], [12, 22, 32], [31, 32, 33]],
                  index=['Name', 'Surnename', 'Age', 'height', 'yob'], columns=['a', 'b', 'c'])

print(df)
# end export to excel

# end of code

Python 3.11.0rc1 (main, Aug 12 2022, 10:02:14) [GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license()" for more information.

======== RESTART: /home/piotr/python/data_base/02_data_base_export_to_excel.py ========
Traceback (most recent call last):
  File "/usr/lib/python3.11/idlelib/run.py", line 578, in runcode
    exec(code, self.locals)
  File "/home/piotr/python/data_base/02_data_base_export_to_excel.py", line 3, in <module>
    import pandas as pd
  File "/usr/lib/python3/dist-packages/pandas/__init__.py", line 22, in <module>
    raise ImportError(
ImportError: Unable to import required dependencies:
numpy: 

IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!

Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.

We have compiled some common reasons and troubleshooting tips at:

    https://numpy.org/devdocs/user/troub...porterror.html

Please note and check the following:

  * The Python version is: Python3.11 from "/usr/bin/python3.11"
  * The NumPy version is: "1.21.5"

and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.

Original error was: No module named 'numpy.core._multiarray_umath'
Bolded a part in your code (and you need to use CODE tags when posting code, to make it easier to read). Did you read that link, and check the things advised?? You don't tell us what version/distro of Linux you're using, or how you installed numpy, or gave us any details. Just posting your code with a VERY well explained error doesn't tell us much.

Provide details, and we can try to help.
 
Old 03-25-2023, 08:20 PM   #3
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,152
Blog Entries: 6

Rep: Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835
After you get numpy fixed.
Code:
import pandas as pd
import openpyxl

# welcome
print('That is simple data base.', 'Please input date.', sep='\n')

# input data
username = input("Name: ")
usersurename = input("Surename: ")
age = input("Enter age: ")
height = input("Enter height: ")
yob = input("Enter YoB: ")

# open file
with open('data_base.txt', 'a') as f:
    f.write(f"\nName: {username}\nSurename: {usersurename}")
    f.write(f"\nAge: {age}\nHeight: {height}\nYob: {yob}\n\n")

# open file data base and print data
with open('data_base.txt', 'r') as f:
    print(f.read())
    
# export to excel
df = pd.DataFrame([[username, usersurename, age, height, yob], 
                        [12, 22, 32], [31, 32, 33]],
index=['Name', 'Surnename', 'Age', 'height', 'yob'], 
                        columns=['a', 'b', 'c'])

print(df)
# end export to excel
# end of code
 
1 members found this post helpful.
Old 03-30-2023, 09:29 AM   #4
piotrbujakowski
LQ Newbie
 
Registered: Jan 2017
Location: London, UK
Distribution: Ubuntu
Posts: 29

Original Poster
Rep: Reputation: 0
Python 3.10.6 (main, Mar 10 2023, 10:55:28) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license()" for more information.
 
Old 03-30-2023, 09:54 AM   #5
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,760

Rep: Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983
Quote:
Originally Posted by piotrbujakowski View Post
Python 3.10.6 (main, Mar 10 2023, 10:55:28) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license()" for more information.
Is this supposed to tell us something??? You had a very clear error, with instructions on how to fix it, and were asked several questions...none of which you answered. If you're not going to answer the questions asked, there isn't going to be anything we can help you with.
 
Old 03-30-2023, 01:09 PM   #6
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,255

Rep: Reputation: 5326Reputation: 5326Reputation: 5326Reputation: 5326Reputation: 5326Reputation: 5326Reputation: 5326Reputation: 5326Reputation: 5326Reputation: 5326Reputation: 5326
Quote:
Code:
# export to excel
df = pd.DataFrame([[username, usersurename, age, height, yob], [12, 22, 32], [31, 32, 33]],
index=['Name', 'Surnename', 'Age', 'height', 'yob'], columns=['a', 'b', 'c'])

print(df)
# end export to excel
Uh, that does not "export to Excel".
 
Old 03-30-2023, 03:35 PM   #7
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,255

Rep: Reputation: 5326Reputation: 5326Reputation: 5326Reputation: 5326Reputation: 5326Reputation: 5326Reputation: 5326Reputation: 5326Reputation: 5326Reputation: 5326Reputation: 5326
Quote:
I have a problem with modules.
I try find how install that and still don't works.
And how did you do that?

If you're using Ubuntu or an Ubuntu-based distro, then the following should work:

Code:
sudo apt-get install python3-pandas
 
Old 03-30-2023, 07:01 PM   #8
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,152
Blog Entries: 6

Rep: Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835
I tested this before posing.

Create Test.db
Code:
#!/usr/bin/python

import sqlite3

try:
    conn = sqlite3.connect("Test.db")
    print("Database Test.db created.")
except:
    print("Database Test.db not created.")
    
c = conn.cursor()
   
c.execute('''CREATE TABLE IF NOT EXISTS files ([files_id] INTEGER PRIMARY KEY, 
            [file_name] TEXT)''')
          
conn.commit()
          
c.execute('''INSERT INTO files (files_id,file_name) VALUES
            (1,'file1'),
            (2,'file2'),
            (3,'file3'),
            (4,'file4'),
            (5,'file5')
          ''')
          
conn.commit()
Make xlsx from Test.db
Code:
#!/usr/bin/python

import sqlite3
import os
import openpyxl
from sqlite3 import Error

DBFILE = "Test.db"

conn = sqlite3.connect(DBFILE)
cursor = conn.cursor()
book = openpyxl.Workbook()
sheet = book.active
 
cursor.execute("SELECT * FROM `files`")
results = cursor.fetchall()
i = 0
for row in results:
    i += 1
    j = 1
for col in row:
    cell = sheet.cell(row = i, column = j)
    cell.value = col
    j += 1
 
book.save("Test.xlsx")
conn.close()
Code:
file Test.db
Test.db: SQLite 3.x database, last written using SQLite version 3040001, file counter 2, database pages 2, cookie 0x1, schema 4, UTF-8, version-valid-for 2

file Test.xlsx
Test.xlsx: Microsoft Excel 2007+
 
Old 03-30-2023, 07:38 PM   #9
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,255

Rep: Reputation: 5326Reputation: 5326Reputation: 5326Reputation: 5326Reputation: 5326Reputation: 5326Reputation: 5326Reputation: 5326Reputation: 5326Reputation: 5326Reputation: 5326
I'd actually advise exporting to CSV (which Excel can open), not directly to Excel, if possible.
 
1 members found this post helpful.
Old 03-30-2023, 08:06 PM   #10
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,152
Blog Entries: 6

Rep: Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835
For python-openpyxl
https://openpyxl.readthedocs.org/
https://et-xmlfile.readthedocs.io/
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading one EXCEL(.xls) file and writing to other EXCEL(.xls) file vijay mishra General 6 04-23-2012 07:53 PM
How to copy Arabic data from excel fiel to another excel file in PERL? David the H. Linux - Newbie 0 04-23-2012 09:24 AM
Convert from XML in excel format to Excel XLS file CPAN module Perseus Programming 9 10-14-2011 12:39 PM
export data to excel or oenoffice.org using Qt pragnya Linux - General 1 01-20-2006 04:55 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration