Hướng dẫn array to excel python - mảng sang excel python

If you are already using pandas for your task, you can use it easily to create a dataframe and turn it into an Excel sheet. If not, it is easier to use xlsxwriter rather than pandas because pandas is a little heavy library.pandas for your task, you can use it easily to create a dataframe and turn it into an Excel sheet. If not, it is easier to use xlsxwriter rather than pandas because pandas is a little heavy library.

pip install XlsxWriter

import xlsxwriter

workbook = xlsxwriter.Workbook("MyExcel.xlsx")
worksheet = workbook.add_worksheet()

There are multiple ways to write the data to excel. You can use write_row(), write_column() or write cell by cell also.

write_row(row, column, data)

row = [1, 2, 3, 4, 5]

worksheet.write_row(1, 2, row)

write_column(row, column, data)

column= [1, 2, 3, 4, 5]

worksheet.write_column(1, 2, column)

write(row, column, data)

worksheet.write(0, 1, "Hello")
worksheet.write(0, 2, "World")

Finally, close the workbook.

workbook.close()

Like this, there are many ways to write data and also you can conditional formatting to your cells or rows or columns. The documentation can be found here.

Ngoài một số vùng chứa chung như List, theo định nghĩa của Python, nó cũng có thể xử lý các vùng chứa với các kiểu dữ liệu được chỉ định. Array có thể được triển khai trong Python bằng mô-đun có tên là “array”. Chúng có thể hữu ích khi chúng ta chỉ dùng một giá trị kiểu dữ liệu cụ thể.array”. Chúng có thể hữu ích khi chúng ta chỉ dùng một giá trị kiểu dữ liệu cụ thể.

1. Các hàm trên Array:Các hàm trên Array:

1. array (kiểu dữ liệu, list giá trị): – Hàm này được sử dụng để tạo một array với kiểu dữ liệu và list giá trị được chỉ định trong các đối số của nó. Một số kiểu dữ liệu được đề cập trong bảng dưới đây.: – Hàm này được sử dụng để tạo một array với kiểu dữ liệu và list giá trị được chỉ định trong các đối số của nó. Một số kiểu dữ liệu được đề cập trong bảng dưới đây.

Type CodeC TypePython TypeMinimum size in Bytes
‘b’ signed char int 1
‘B’ unsigned char int 1
‘B’ unsigned char ‘u’ 2
Py_UNICODE unicode character int 2
‘B’ unsigned char int 2
‘B’ unsigned char int 2
‘B’ unsigned char int 2
‘B’ unsigned char int 4
‘B’ unsigned char int 4
‘B’ unsigned char int 8
‘B’ unsigned char int 8
‘B’ unsigned char unsigned char 4
‘u’ Py_UNICODE unsigned char 8

‘u’ : – Hàm này được sử dụng để thêm giá trị được đề cập trong các đối số của nó ở cuối array.

Py_UNICODE : – Hàm này được sử dụng để thêm giá trị tại vị trí được chỉ định trong đối số của nó.

# -----------------------------------------------------------
#Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
#@author cafedevn
#Contact: 
#Fanpage: https://www.facebook.com/cafedevn
#Group: https://www.facebook.com/groups/cafedev.vn/
#Instagram: https://instagram.com/cafedevn
#Twitter: https://twitter.com/CafedeVn
#Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
#Pinterest: https://www.pinterest.com/cafedevvn/
#YouTube: https://www.youtube.com/channel/UCE7zpY_SlHGEgo67pHxqIoA/
# -----------------------------------------------------------


# Python code to demonstrate the working of  
# array(), append(), insert() 
   
# importing "array" for array operations 
import array 
   
# initializing array with array values 
# initializes array with signed integers 
arr = array.array('i', [1, 2, 3])  
  
# printing original array 
print ("The new created array is : ",end=" ") 
for i in range (0, 3): 
    print (arr[i], end=" ") 
  
print("\r") 
  
# using append() to insert new value at end 
arr.append(4); 
  
# printing appended array 
print("The appended array is : ", end="") 
for i in range (0, 4): 
    print (arr[i], end=" ") 
      
# using insert() to insert value at specific position 
# inserts 5 at 2nd position 
arr.insert(2, 5) 
  
print("\r") 
  
# printing array after insertion 
print ("The array after insertion is : ", end="") 
for i in range (0, 5): 
    print (arr[i], end=" ") 

unicode character

The new created array is : 1 2 3 
The appended array is : 1 2 3 4 
The array after insertion is : 1 2 5 3 4 

‘h’ : – Hàm này xóa các phần tử tại vị trí được đề cập trong đối số của nó và trả về nó.

signed short : – Hàm này được sử dụng để xóa sự xuất hiện đầu tiên của giá trị được đề cập trong các đối số của nó.


# Python code to demonstrate the working of  
# pop() and remove() 
   
# importing "array" for array operations 
import array 
   
# initializing array with array values 
# initializes array with signed integers 
arr= array.array('i',[1, 2, 3, 1, 5])  
  
# printing original array 
print ("The new created array is : ",end="") 
for i in range (0,5): 
    print (arr[i],end=" ") 
  
print ("\r") 
  
# using pop() to remove element at 2nd position 
print ("The popped element is : ",end="") 
print (arr.pop(2)); 
  
# printing array after popping 
print ("The array after popping is : ",end="") 
for i in range (0,4): 
    print (arr[i],end=" ") 
  
print("\r") 
  
# using remove() to remove 1st occurrence of 1 
arr.remove(1) 
  
# printing array after removing 
print ("The array after removing is : ",end="") 
for i in range (0,3): 
    print (arr[i],end=" ") 

unicode character

The new created array is : 1 2 3 1 5 
The popped element is : 3
The array after popping is : 1 2 1 5 
The array after removing is : 2 1 5 

‘h’  : – Hàm này trả về chỉ mục của lần xuất hiện đầu tiên của giá trị được đề cập trong các đối số.

signed short  : – Hàm này đảo ngược array lại.

# -----------------------------------------------------------
#Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
#@author cafedevn
#Contact: 
#Fanpage: https://www.facebook.com/cafedevn
#Group: https://www.facebook.com/groups/cafedev.vn/
#Instagram: https://instagram.com/cafedevn
#Twitter: https://twitter.com/CafedeVn
#Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
#Pinterest: https://www.pinterest.com/cafedevvn/
#YouTube: https://www.youtube.com/channel/UCE7zpY_SlHGEgo67pHxqIoA/
# -----------------------------------------------------------


# Python code to demonstrate the working of  
# index() and reverse() 
   
# importing "array" for array operations 
import array 
   
# initializing array with array values 
# initializes array with signed integers 
arr= array.array('i',[1, 2, 3, 1, 2, 5])  
  
# printing original array 
print ("The new created array is : ",end="") 
for i in range (0,6): 
    print (arr[i],end=" ") 
  
print ("\r") 
  
# using index() to print index of 1st occurrenece of 2 
print ("The index of 1st occurrence of 2 is : ",end="") 
print (arr.index(2)) 
  
#using reverse() to reverse the array 
arr.reverse() 
  
# printing array after reversing 
print ("The array after reversing is : ",end="") 
for i in range (0,6): 
    print (arr[i],end=" ") 

‘H’

unsigned short

‘i’

signed int

  • w3school
  • python.org
  • ‘I’

unsigned int

  • ‘l’
  • Ebook về python tại đây.
  • Các series tự học lập trình khác

Nếu bạn thấy hay và hữu ích, bạn có thể tham gia các kênh sau của cafedev để nhận được nhiều hơn nữa:

  • Group Facebook
  • Fanpage
  • Youtube
  • Instagram
  • Twitter
  • Linkedin
  • Pinterest
  • Trang chủ

Chào thân ái và quyết thắng!

Đăng ký kênh youtube để ủng hộ Cafedev nha các bạn, Thanks you!