突然碰见一个问题,就是txt文本内的内容需要切分后按条件变成xls,比如如下文献需要按“.”
# Xiao-Wei Xu, Xiao-Xin Wu, Xian-Gao Jiang, Kai-Jin Xu, Ling-Jun Ying, Chun-Lian Ma, Shi-Bo Li, Hua-Ying Wang, Sheng Zhang, Hai-Nv Gao, Ji-Fang Sheng, Hong-Liu Cai, Yun-Qing Qiu, Lan-Juan Li. Clinical findings in a group of patients infected with the 2019 novel coronavirus (SARS-Cov-2) outside of Wuhan, China: retrospective case series. BMJ. 2020 Feb 19;368:m606.
因此python就可以起作用了,代码如下:
import xlwt
import codecs
input_txt = '/Users/xujun/Downloads/1.txt'
output_excel = '/Users/xujun/Downloads/2.xls'
sheetName = 'Sheet1'
start_row = 0
start_col = 0
wb = xlwt.Workbook(encoding = 'utf-8')
ws = wb.add_sheet(sheetName)
f = open(input_txt, encoding = 'utf-8')
row_excel = start_row
for line in f:
line = line.strip('\n')
line = line.split('.')
print(line)
col_excel = start_col
len_line = len(line)
for j in range(len_line):
print (line[j])
ws.write(row_excel,col_excel,line[j])
col_excel += 1
wb.save(output_excel)
row_excel += 1
f.close
亲测有效!
原创文章(本站视频密码:66668888),作者:xujunzju,如若转载,请注明出处:https://zyicu.cn/?p=6853