就差一个Python写Excel两列的操作了,吃货不会了。
# -*- coding:utf-8 -*-import sys
from xlrd import open_workbook # xlrd用于读取xld
import xlwt # 用于写入xls
workbook = open_workbook(r'C:\88.xls')# 打开xls文件
sheet_name= workbook.sheet_names()# 打印所有sheet名称,是个列表
sheet = workbook.sheet_by_index(0)# 根据sheet索引读取sheet中的所有内容
sheet1= workbook.sheet_by_name('Sheet1')# 根据sheet名称读取sheet中的所有内容
print(sheet.name, sheet.nrows, sheet.ncols)# sheet的名称、行数、列数
#content = sheet.col_values(1)# 第1列内容
#print(content)
for i in range(sheet.nrows):
def tranlate(source, direction):
import requests
import json
url = "http://api.interpreter.caiyunai.com/v1/translator"
#WARNING, this token is a test token for new developers, and it should be replaced by your token
token = "法王的令牌"
payload = {
"source" : source,
"trans_type" : direction,
"request_id" : "demo",
"detect": True,
}
headers = {
'content-type': "application/json",
'x-authorization': "token " + token,
}
response = requests.request("POST", url, data=json.dumps(payload), headers=headers)
return json.loads(response.text)['target']
#print (sheet1.cell_value(i, 0)) #循环打印第1列,第2列内容: sheet1.cell_value(i, 1)
AAA= sheet1.cell_value(i, 0)
target = tranlate(AAA, "auto2zh")
print(target)
下面还差一个写入到excel文件的操作
就是原义列
和
翻译结果列!
来看看哈 直接用pandas呀,data我一般是是列表嵌套字典格式
import pandas as pd
def to_excel(data):
df = pd.DataFrame(data)
df.to_excel("result.xlsx"), index=False) 可以考虑pandas的to_excel 加油每日一小步罗马就在前面我还没开始学表格{:1_921:} 13286246768 发表于 2021-6-2 08:42
直接用pandas呀,data我一般是是列表嵌套字典格式
import pandas as pd
@13286246768
给我改一下吧,叔!{:301_1003:} def tranlate(source, direction)
函数写到最外层啊 {:1_907:}还是易语言香这一堆堆的代码 让我这个英语不及格的人很少头痛..
JavaScript和java就够让我头痛的了. 冥界3大法王 发表于 2021-6-2 08:46
@13286246768
给我改一下吧,叔!
# -*- coding:utf-8 -*-
import sys
from xlrd import open_workbook# xlrd用于读取xld
import pandas as pd
import xlwt# 用于写入xls
workbook = open_workbook(r'C:\88.xls')# 打开xls文件
sheet_name = workbook.sheet_names()# 打印所有sheet名称,是个列表
sheet = workbook.sheet_by_index(0)# 根据sheet索引读取sheet中的所有内容
sheet1 = workbook.sheet_by_name('Sheet1')# 根据sheet名称读取sheet中的所有内容
print(sheet.name, sheet.nrows, sheet.ncols)# sheet的名称、行数、列数
# content = sheet.col_values(1)# 第1列内容
# print(content)
def tranlate(source, direction):
import requests
import json
url = "http://api.interpreter.caiyunai.com/v1/translator"
# WARNING, this token is a test token for new developers, and it should be replaced by your token
token = "法王的令牌"
payload = {
"source": source,
"trans_type": direction,
"request_id": "demo",
"detect": True,
}
headers = {
'content-type': "application/json",
'x-authorization': "token " + token,
}
response = requests.request("POST", url, data=json.dumps(payload), headers=headers)
# print (sheet1.cell_value(i, 0)) #循环打印第1列,第2列内容: sheet1.cell_value(i, 1)
return json.loads(response.text)['target']
def to_excel(data):
df = pd.DataFrame(data)
df.to_excel("result.xlsx", index=False)
tranlate_datas = []
for i in range(sheet.nrows):
tranlate_dict = {}
AAA = sheet1.cell_value(i, 0)
target = tranlate(AAA, "auto2zh")
tranlate_dict['name1'] = AAA
tranlate_dict['name2'] = target
tranlate_datas.append(tranlate_dict)
print(target)
to_excel(tranlate_datas)
页:
[1]
2