1.0版本
import faker
from openpyxl import load_workbook
from openpyxl import Workbook
from openpyxl.styles import Alignment
import csv
def csv_to_xlsx():
with open('rawdatapy.csv', 'r', encoding='utf-8') as f:
read = csv.reader(f)
wb = Workbook()
ws = wb.active
for line in read:
ws.append(line)
wb.save('rawdatapy.xlsx')
print("---将原始的csv文件转化为xlsx文件---")
def xlsx_to_csv():
ob = csv.writer(open("result.csv", 'w', newline="", encoding='utf-8'))
data = load_workbook('rawdatapy.xlsx').active
for r in data.rows:
row = [a.value for a in r]
ob.writerow(row)
print("---成功将xlsx转化为csv文件---")
def write_data(a=3, b=20, c=300):
wb = Workbook()
my_sheet = wb.active
my_sheet.title = "data"
my_sheet.cell(1, 1, str("城市"))
my_sheet.cell(1, 2, str("街道"))
my_sheet.cell(1, 3, str("地址"))
city_list = []
street_list = []
cnt = 2
for i in range(0, a):
f = faker.Faker(locale='zh_CN')
while city_list.count(f.city_name()) > 0:
f = faker.Faker(locale='zh_CN')
city = f.city_name()
city_list.append(city)
for j in range(0, b):
f = faker.Faker(locale='zh_CN')
while street_list.count(f.street_name()) > 0:
f = faker.Faker(locale='zh_CN')
street = f.street_name()
street_list.append(street)
for k in range(0, c):
f = faker.Faker(locale='zh_CN')
addr = f.address()
my_sheet.cell(row=cnt, column=1, value=str(city))
my_sheet.cell(row=cnt, column=2, value=str(street))
my_sheet.cell(row=cnt, column=3, value=str(addr))
cnt = cnt + 1
wb.save("rawdatapy.xlsx")
print("xlsx格式表格写入数据成功!")
wb.close()
def read_data(num=2):
wb1 = load_workbook("rawdatapy.xlsx")
my_sheet = wb1.worksheets[0]
rows = my_sheet.max_row
need_to_merge = []
for i in range(1, num+1):
dict_from = {}
for ii in range(2, rows + 1):
r = ""
if i == 1:
r = str(my_sheet.cell(ii, i).value)
if i == 2:
r = str(my_sheet.cell(ii, i-1).value) + str(my_sheet.cell(ii, i).value)
if i == 3:
r = str(my_sheet.cell(ii, i-2).value) + str(my_sheet.cell(ii, i-1).value) + str(my_sheet.cell(ii, i).value)
if not dict_from.get(r):
dict_from[r] = [i, ii, ii]
else:
dict_from[r][2] = dict_from[r][2] + 1
dict_cross = {1: 'A', 2: 'B', 3: 'C'}
for key, value in dict_from.items():
list_value0 = dict_cross[value[0]]
if not (f'{list_value0}{value[1]}' == f'{list_value0}{value[2]}'):
temp = [f'{list_value0}{value[1]}', f'{list_value0}{value[2]}']
need_to_merge.append(temp)
for kk in need_to_merge:
my_sheet.merge_cells(f'{kk[0]}:{kk[1]}')
my_sheet[f'{kk[0]}'].alignment = Alignment(horizontal='center', vertical='center')
wb1.save("rawdatapy.xlsx")
wb1.close()
print("--合并结束finish--")
if __name__ == '__main__':
read_data()
1.1 版本 新思路实现
import faker
from openpyxl import load_workbook
from openpyxl import Workbook
from openpyxl.styles import Alignment
import csv
def csv_to_xlsx():
with open('rawdatapy.csv', 'r', encoding='utf-8') as f:
read = csv.reader(f)
wb = Workbook()
ws = wb.active
for line in read:
ws.append(line)
wb.save('rawdatapy.xlsx')
print("---将原始的csv文件转化为xlsx文件---")
def xlsx_to_csv():
ob = csv.writer(open("result.csv", 'w', newline="", encoding='utf-8'))
data = load_workbook('rawdatapy.xlsx').active
for r in data.rows:
row = [a.value for a in r]
ob.writerow(row)
print("---成功将xlsx转化为csv文件---")
def write_data(a=3, b=20, c=300):
wb = Workbook()
my_sheet = wb.active
my_sheet.title = "data"
my_sheet.cell(1, 1, str("城市"))
my_sheet.cell(1, 2, str("街道"))
my_sheet.cell(1, 3, str("地址"))
city_list = []
street_list = []
cnt = 2
for i in range(0, a):
f = faker.Faker(locale='zh_CN')
while city_list.count(f.city_name()) > 0:
f = faker.Faker(locale='zh_CN')
city = f.city_name()
city_list.append(city)
for j in range(0, b):
f = faker.Faker(locale='zh_CN')
while street_list.count(f.street_name()) > 0:
f = faker.Faker(locale='zh_CN')
street = f.street_name()
street_list.append(street)
for k in range(0, c):
f = faker.Faker(locale='zh_CN')
addr = f.address()
my_sheet.cell(row=cnt, column=1, value=str(city))
my_sheet.cell(row=cnt, column=2, value=str(street))
my_sheet.cell(row=cnt, column=3, value=str(addr))
cnt = cnt + 1
wb.save("rawdatapy.xlsx")
print("xlsx格式表格写入数据成功!")
wb.close()
def read_data(num=2):
wb1 = load_workbook("rawdatapy.xlsx")
my_sheet = wb1.worksheets[0]
rows = my_sheet.max_row
cols = my_sheet.max_column
help_array = [[0 for i in range(num)] for j in range(rows)]
for i in range(1, num + 1):
cnt = 0
for j in range(2, rows + 1):
k_new = str(my_sheet.cell(j, i).value)
k_old = str(my_sheet.cell(j - 1, i).value)
if k_old != k_new:
cnt = cnt + 1
help_array[j - 1][i - 1] = cnt
for i in range(0, num):
dict_from = {}
for ii in range(0, rows):
r = ""
if i == 0:
r = str(help_array[ii][i])
if i == 1:
r = str(help_array[ii][i-1]) + str(help_array[ii][i])
if i == 2:
r = str(help_array[ii][i-2]) + str(help_array[ii][i-1]) + str(help_array[ii][i])
if not dict_from.get(r):
dict_from[r] = [i, ii, ii]
else:
dict_from[r][2] = dict_from[r][2] + 1
dict_cross = {1: 'A', 2: 'B', 3: 'C'}
for key, value in dict_from.items():
list_value0 = dict_cross[value[0]+1]
if not (f'{list_value0}{value[1]+1}' == f'{list_value0}{value[2]+1}'):
my_sheet.merge_cells(f'{list_value0}{value[1]+1}:{list_value0}{value[2]+1}')
my_sheet[f'{list_value0}{value[1]+1}'].alignment = Alignment(horizontal='center', vertical='center')
wb1.save("rawdatapy.xlsx")
wb1.close()
print("--合并结束finish--")
if __name__ == '__main__':
read_data(3)
1.2 最新版本 优化
import faker
from openpyxl import load_workbook
from openpyxl import Workbook
from openpyxl.styles import Alignment
import csv
def csv_to_xlsx():
with open('rawdatapy.csv', 'r', encoding='utf-8') as f:
read = csv.reader(f)
wb = Workbook()
ws = wb.active
for line in read:
ws.append(line)
wb.save('rawdatapy.xlsx')
print("---将原始的csv文件转化为xlsx文件---")
def xlsx_to_csv():
ob = csv.writer(open("result.csv", 'w', newline="", encoding='utf-8'))
data = load_workbook('rawdatapy.xlsx').active
for r in data.rows:
row = [a.value for a in r]
ob.writerow(row)
print("---成功将xlsx转化为csv文件---")
def write_data(a=3, b=20, c=300):
wb = Workbook()
my_sheet = wb.active
my_sheet.title = "data"
my_sheet.cell(1, 1, str("城市"))
my_sheet.cell(1, 2, str("街道"))
my_sheet.cell(1, 3, str("地址"))
city_list = []
street_list = []
cnt = 2
for i in range(0, a):
f = faker.Faker(locale='zh_CN')
while city_list.count(f.city_name()) > 0:
f = faker.Faker(locale='zh_CN')
city = f.city_name()
city_list.append(city)
for j in range(0, b):
f = faker.Faker(locale='zh_CN')
while street_list.count(f.street_name()) > 0:
f = faker.Faker(locale='zh_CN')
street = f.street_name()
street_list.append(street)
for k in range(0, c):
f = faker.Faker(locale='zh_CN')
addr = f.address()
my_sheet.cell(row=cnt, column=1, value=str(city))
my_sheet.cell(row=cnt, column=2, value=str(street))
my_sheet.cell(row=cnt, column=3, value=str(addr))
cnt = cnt + 1
wb.save("rawdatapy.xlsx")
print("xlsx格式表格写入数据成功!")
wb.close()
def read_data(num=2):
wb1 = load_workbook("rawdatapy.xlsx")
my_sheet = wb1.worksheets[0]
rows = my_sheet.max_row
cols = my_sheet.max_column
help_array = [[0 for i in range(num)] for j in range(rows)]
for i in range(1, num + 1):
cnt = 0
for j in range(2, rows + 1):
k_new = str(my_sheet.cell(j, i).value)
k_old = str(my_sheet.cell(j - 1, i).value)
if k_old != k_new:
cnt = cnt + 1
help_array[j - 1][i - 1] = cnt
dict_cross = {}
for i in range(1, num+1):
dict_cross[i] = chr(ord('A')+i-1)
for i in range(0, num):
dict_from = {}
for ii in range(0, rows):
r = ""
for k in range(0, i+1):
r = r + ' ' + str(help_array[ii][k])
if not dict_from.get(r):
dict_from[r] = [i, ii, ii]
else:
dict_from[r][2] = dict_from[r][2] + 1
for key, value in dict_from.items():
letter = dict_cross[value[0]+1]
if not (f'{letter}{value[1]+1}' == f'{letter}{value[2]+1}'):
my_sheet.merge_cells(f'{letter}{value[1]+1}:{letter}{value[2]+1}')
my_sheet[f'{letter}{value[1]+1}'].alignment = Alignment(horizontal='center', vertical='center')
wb1.save("rawdatapy.xlsx")
wb1.close()
print("--合并结束finish--")
if __name__ == '__main__':
read_data(3)
1.3版本 优化变量名称
import faker
from openpyxl import load_workbook
from openpyxl import Workbook
from openpyxl.styles import Alignment
import csv
def csv_to_xlsx():
with open('rawdatapy.csv', 'r', encoding='utf-8') as f:
read = csv.reader(f)
wb = Workbook()
ws = wb.active
for line in read:
ws.append(line)
wb.save('rawdatapy.xlsx')
print("---将原始的csv文件转化为xlsx文件---")
def xlsx_to_csv():
ob = csv.writer(open("result.csv", 'w', newline="", encoding='utf-8'))
data = load_workbook('rawdatapy.xlsx').active
for r in data.rows:
row = [a.value for a in r]
ob.writerow(row)
print("---成功将xlsx转化为csv文件---")
def write_data(a=3, b=20, c=300):
wb = Workbook()
my_sheet = wb.active
my_sheet.title = "data"
my_sheet.cell(1, 1, str("城市"))
my_sheet.cell(1, 2, str("街道"))
my_sheet.cell(1, 3, str("地址"))
city_list = []
street_list = []
cnt = 2
for i in range(0, a):
f = faker.Faker(locale='zh_CN')
while city_list.count(f.city_name()) > 0:
f = faker.Faker(locale='zh_CN')
city = f.city_name()
city_list.append(city)
for j in range(0, b):
f = faker.Faker(locale='zh_CN')
while street_list.count(f.street_name()) > 0:
f = faker.Faker(locale='zh_CN')
street = f.street_name()
street_list.append(street)
for k in range(0, c):
f = faker.Faker(locale='zh_CN')
addr = f.address()
my_sheet.cell(row=cnt, column=1, value=str(city))
my_sheet.cell(row=cnt, column=2, value=str(street))
my_sheet.cell(row=cnt, column=3, value=str(addr))
cnt = cnt + 1
wb.save("rawdatapy.xlsx")
print("xlsx格式表格写入数据成功!")
wb.close()
def read_data(num=2):
wb1 = load_workbook("rawdatapy.xlsx")
my_sheet = wb1.worksheets[0]
MaxRows = my_sheet.max_row
help_array = [[0 for i in range(num)] for j in range(MaxRows)]
for i in range(1, num + 1):
cnt = 0
for j in range(2, MaxRows + 1):
k_new = str(my_sheet.cell(j, i).value)
k_old = str(my_sheet.cell(j - 1, i).value)
if k_old != k_new:
cnt = cnt + 1
help_array[j - 1][i - 1] = cnt
letterList = {}
for i in range(1, num+1):
letterList[i] = chr(ord('A')+i-1)
for i in range(0, num):
dictList = {}
for row in range(0, MaxRows):
r = ""
for k in range(0, i+1):
r = r + ' ' + str(help_array[row][k])
if not dictList.get(r):
dictList[r] = [i, row, row]
else:
dictList[r][2] = dictList[r][2] + 1
for key, value in dictList.items():
letter = letterList[value[0]+1]
if not (f'{letter}{value[1]+1}' == f'{letter}{value[2]+1}'):
my_sheet.merge_cells(f'{letter}{value[1]+1}:{letter}{value[2]+1}')
my_sheet[f'{letter}{value[1]+1}'].alignment = Alignment(horizontal='center', vertical='center')
wb1.save("rawdatapy.xlsx")
wb1.close()
print("--合并结束finish--")
if __name__ == '__main__':
read_data(3)