python如何将txt文件导入excel 怎么用python读取txt文件里指定行的内容,并导入exc...

\u600e\u4e48\u7528python\u8bfb\u53d6txt\u6587\u4ef6\u91cc\u6307\u5b9a\u884c\u7684\u5185\u5bb9\uff0c\u5e76\u5bfc\u5165excel

\u4e00\u3001\u8bfbexcel\u8868
\u8bfbexcel\u8981\u7528\u5230xlrd\u6a21\u5757\uff0c\u5b98\u7f51\u5b89\u88c5\uff08http://pypi.python.org/pypi/xlrd\uff09\u3002\u7136\u540e\u5c31\u53ef\u4ee5\u8ddf\u7740\u91cc\u9762\u7684\u4f8b\u5b50\u7a0d\u5fae\u8bd5\u4e00\u4e0b\u5c31\u77e5\u9053\u600e\u4e48\u7528\u4e86\u3002\u5927\u6982\u7684\u6d41\u7a0b\u662f\u8fd9\u6837\u7684\uff1a
1\u3001\u5bfc\u5165\u6a21\u5757

\u590d\u5236\u4ee3\u7801\u4ee3\u7801\u5982\u4e0b:
import xlrd

2\u3001\u6253\u5f00Excel\u6587\u4ef6\u8bfb\u53d6\u6570\u636e

\u590d\u5236\u4ee3\u7801\u4ee3\u7801\u5982\u4e0b:
data = xlrd.open_workbook('excel.xls')

3\u3001\u83b7\u53d6\u4e00\u4e2a\u5de5\u4f5c\u8868
\u2460 table = data.sheets()[0] #\u901a\u8fc7\u7d22\u5f15\u987a\u5e8f\u83b7\u53d6
\u2461 table = data.sheet_by_index(0) #\u901a\u8fc7\u7d22\u5f15\u987a\u5e8f\u83b7\u53d6
\u2462 table = data.sheet_by_name(u'Sheet1')#\u901a\u8fc7\u540d\u79f0\u83b7\u53d6
4\u3001\u83b7\u53d6\u6574\u884c\u548c\u6574\u5217\u7684\u503c\uff08\u8fd4\u56de\u6570\u7ec4\uff09

\u590d\u5236\u4ee3\u7801\u4ee3\u7801\u5982\u4e0b:
table.row_values(i)
table.col_values(i)

5\u3001\u83b7\u53d6\u884c\u6570\u548c\u5217\u6570\u3000

\u590d\u5236\u4ee3\u7801\u4ee3\u7801\u5982\u4e0b:
table.nrows
table.ncols

6\u3001\u83b7\u53d6\u5355\u5143\u683c

\u590d\u5236\u4ee3\u7801\u4ee3\u7801\u5982\u4e0b:
table.cell(0,0).value
table.cell(2,3).value

\u5c31\u6211\u81ea\u5df1\u4f7f\u7528\u7684\u65f6\u5019\u89c9\u5f97\u8fd8\u662f\u83b7\u53d6cell\u6700\u6709\u7528\uff0c\u8fd9\u5c31\u76f8\u5f53\u4e8e\u662f\u7ed9\u4e86\u4f60\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\uff0c\u4f59\u4e0b\u4f60\u5c31\u53ef\u4ee5\u60f3\u600e\u4e48\u5e72\u5c31\u600e\u4e48\u5e72\u4e86\u3002\u5f97\u76ca\u4e8e\u8fd9\u4e2a\u5341\u5206\u597d\u7528\u7684\u5e93\u4ee3\u7801\u5f88\u662f\u7b80\u6d01\u3002\u4f46\u662f\u8fd8\u662f\u6709\u82e5\u5e72\u5751\u7684\u5b58\u5728\u5bfc\u81f4\u8bdd\u4e86\u4e00\u5b9a\u65f6\u95f4\u63a2\u7d22\u3002\u73b0\u5728\u5217\u51fa\u6765\u4f9b\u540e\u4eba\u53c2\u8003\u5427\uff1a

1\u3001\u9996\u5148\u5c31\u662f\u6211\u7684\u7edf\u8ba1\u662f\u6839\u636e\u59d3\u540d\u7edf\u8ba1\u5404\u4e2a\u8868\u4e2d\u7684\u4fe1\u606f\u7684\uff0c\u4f46\u662f\u8c03\u8bd5\u53d1\u73b0\u4e0d\u540c\u7684\u8868\u4e2d\u5404\u4e2a\u540d\u5b57\u8c8c\u4f3c\u4e0d\u80fd\u591f\u5339\u914d\uff0c\u5f00\u59cb\u6000\u7591\u8fc7\u7f16\u7801\u95ee\u9898\uff0c\u4e0d\u8fc7\u540e\u6765\u53d1\u73b0\u662f\u56e0\u4e3a\u3000\u3000\u7a7a\u683c\u3002\u56e0\u4e3a\u5728excel\u4e2d\u8f93\u5165\u7684\u65f6\u5019\u5f88\u53ef\u80fd\u4f1a\u987a\u624b\u5728\u4e00\u4e9b\u540d\u5b57\u540e\u9762\u52a0\u4e0a\u51e0\u4e2a\u7a7a\u683c\u6216\u662ftab\u952e\uff0c\u8fd9\u6837\u770b\u8d77\u6765\u6ca1\u4ec0\u4e48\u5dee\u522b\uff0c\u4f46\u662f\u7a0b\u5e8f\u5904\u7406\u7684\u65f6\u5019\u8fd9\u5c31\u662f\u4e24\u4e2a\u5b8c\u5168\u3000\u3000\u4e0d\u540c\u7684\u4e32\u4e86\u3002\u6211\u7684\u89e3\u51b3\u65b9\u6cd5\u662f\u7ed9\u6bcf\u4e2a\u83b7\u53d6\u7684\u5b57\u7b26\u4e32\u90fd\u52a0\u4e0astrip\uff08\uff09\u5904\u7406\u4e00\u4e0b\u3002\u6548\u679c\u826f\u597d
2\u3001\u8fd8\u662f\u5b57\u7b26\u4e32\u7684\u5339\u914d\uff0c\u5728\u5224\u65ad\u67d0\u4e2a\u5355\u5143\u683c\u4e2d\u7684\u5b57\u7b26\u4e32\uff08\u4e2d\u6587\uff09\u662f\u5426\u7b49\u4e8e\u6211\u6240\u7ed9\u51fa\u7684\u7684\u65f6\u5019\u53d1\u73b0\u65e0\u6cd5\u5339\u914d\uff0c\u5e76\u4e14\u5404\u79cdunicode\u4e5f\u4e0d\u592a\u594f\u6548\uff0c\u767e\u5ea6\u8fc7\u4e00\u4e9b\u89e3\u51b3\u3000\u3000\u65b9\u6848\uff0c\u4f46\u662f\u90fd\u6bd4\u8f83\u590d\u6742\u6216\u662f\u6ca1\u7528\u3002\u6700\u540e\u6211\u91c7\u7528\u4e86\u4e00\u4e2a\u6bd4\u8f83\u53d8\u901a\u7684\u65b9\u5f0f\uff1a\u76f4\u63a5\u4eceexcel\u4e2d\u83b7\u53d6\u6211\u60f3\u8981\u7684\u503c\u518d\u8fdb\u884c\u6bd4\u8f83\uff0c\u6548\u679c\u662f\u4e0d\u9519\u5c31\u662f\u901a\u7528\u884c\u4e0d\u592a\u597d\uff0c\u4e2a\u3000\u3000\u5462\u4e0d\u80fd\u95ee\u9898\u8fd8\u6ca1\u89e3\u51b3\u3002
\u4e8c\u3001\u5199excel\u8868
\u5199excel\u8868\u8981\u7528\u5230xlwt\u6a21\u5757\uff0c\u5b98\u7f51\u4e0b\u8f7d\uff08http://pypi.python.org/pypi/xlwt\uff09\u3002\u5927\u81f4\u4f7f\u7528\u6d41\u7a0b\u5982\u4e0b\uff1a
1\u3001\u5bfc\u5165\u6a21\u5757

\u590d\u5236\u4ee3\u7801\u4ee3\u7801\u5982\u4e0b:
import xlwt

2\u3001\u521b\u5efaworkbook\uff08\u5176\u5b9e\u5c31\u662fexcel\uff0c\u540e\u6765\u4fdd\u5b58\u4e00\u4e0b\u5c31\u884c\uff09

\u590d\u5236\u4ee3\u7801\u4ee3\u7801\u5982\u4e0b:
workbook = xlwt.Workbook(encoding = 'ascii')

3\u3001\u521b\u5efa\u8868

\u590d\u5236\u4ee3\u7801\u4ee3\u7801\u5982\u4e0b:
worksheet = workbook.add_sheet('My Worksheet')

4\u3001\u5f80\u5355\u5143\u683c\u5185\u5199\u5165\u5185\u5bb9

\u590d\u5236\u4ee3\u7801\u4ee3\u7801\u5982\u4e0b:
worksheet.write(0, 0, label = 'Row 0, Column 0 Value')

5\u3001\u4fdd\u5b58

\u590d\u5236\u4ee3\u7801\u4ee3\u7801\u5982\u4e0b:
workbook.save('Excel_Workbook.xls')

\u7531\u4e8e\u6211\u7684\u9700\u6c42\u6bd4\u8f83\u7b80\u5355\uff0c\u6240\u4ee5\u8fd9\u4e0a\u9762\u6ca1\u9047\u5230\u4ec0\u4e48\u95ee\u9898\uff0c\u552f\u4e00\u7684\u5c31\u662f\u5efa\u8bae\u8fd8\u662f\u7528ascii\u7f16\u7801\uff0c\u4e0d\u7136\u53ef\u80fd\u4f1a\u6709\u4e00\u4e9b\u8be1\u5f02\u7684\u73b0\u8c61\u3002

\u5f53\u7136xlwt\u529f\u80fd\u8fdc\u8fdc\u4e0d\u6b62\u8fd9\u4e9b\uff0c\u4ed6\u751a\u81f3\u53ef\u4ee5\u8bbe\u7f6e\u5404\u79cd\u6837\u5f0f\u4e4b\u7c7b\u7684\u3002\u9644\u4e0a\u4e00\u70b9\u4f8b\u5b50

\u590d\u5236\u4ee3\u7801\u4ee3\u7801\u5982\u4e0b:

Examples Generating Excel Documents Using Python's xlwt

Here are some simple examples using Python's xlwt library to dynamically generate Excel documents.
Please note a useful alternative may be ezodf, which allows you to generate ODS (Open Document Spreadsheet) files for LibreOffi

\u5168\u6587\u4f7f\u7528\u7684\u662fxlswriter\u6a21\u5757\u5199\u7684\uff0c\u4e5f\u6709\u4eba\u4f7f\u7528 xlrd\u4e0e xlutils\u6a21\u5757\u5b9e\u73b0\uff0c\u4e0d\u8fc7\u8fd8\u672a\u8fdb\u884c\u9a8c\u8bc1
import xlsxwriter
workbook = xlsxwriter.Workbook("D:\\Program Files\\subpy\\sql2.xlsx")#\u5728\u6307\u5b9a\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2aexcle
worksheet = workbook.add_worksheet("students")#\u65b0\u5efa\u4e00\u4e2asheet
title_index = ["A","B","C","D"]#sheet\u4e2d\u7684\u533a\u57df
li = [] #\u5b9a\u4e49\u4e00\u4e2a\u7a7a\u5217\u8868
blod = workbook.add_format({"bold":True})#\u5b9a\u4e49exlce\u4e2d\u5199\u5165\u7684\u5b57\u4f53with open("D:\\Program Files\\subpy\\tets.txt",'r') as f1:#\u6253\u5f00txt\u6587\u6863
lines = f1.readlines()#\u8bfb\u53d6\u6240\u6709\u884c\u5185\u5bb9
n = -1#\u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf
for x in lines:#\u9010\u884c\u8bfb\u53d6
n=n+1
li.append(x[:-1]\uff09#\u53bb\u6389\u56de\u8f66\u7b26
y= x.split#\u4ee5\u7a7a\u683c\u5206\u5b57\u7b26
for i in range(len(title_index)):#\u8bfb\u53d6excle\u533a\u57df\u4e0b\u6807
# for i,j in enumerate(title_index)\uff1a
content = y[i]#\u5355\u4e2a\u5b57\u7b26\u8bfb\u53d6
worksheet.write(n,i,content,blod)#\u5206\u884c\u5206\u5217\u5199\u5165workbook.
close#\u5173\u95edexcle
txt\u6587\u4ef6\u53ef\u4ee5\u7528\u884c\u53f7\uff0c\u7528readlines\u8bfb\u53d6\u51fa\u6765\u7684\u6570\u636e\u662f\u4e00\u4e2a\u5217\u8868\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\uff1a
f = open('', 'r')
line = f.readlines()
line_need = line[\u884c\u53f7-1]
\u8fd9\u6837\u6765\u53d6\u6307\u5b9a\u884c

# -*- coding: utf-8 -*-
import xlwt
x=xlwt.Workbook()
s1=x.add_sheet('sheet1')

style1=xlwt.XFStyle() #样式类
style1.font.colour_index=30 #字体颜色前景色为红
style1.font.bold=True #粗体
style1.pattern.pattern=1 #填充solid
style1.pattern.pattern_fore_colour=2 #填充颜色红色
style2=xlwt.easyxf('font:italic on;pattern:pattern solid,fore-colour yellow')
#快速生成样式
#参数字符串格式: 
#"class1:key1 value1,key2 value2;class2:k1 v1,k2 v2;"

s1.write(0,0,"Hello",style1)#写入字符串
s1.write(1,0,True,style2)#写入真值
s1.write(2,0,3.1415926);s1.write(2,1,-5);s1.write(2,2,xlwt.Formula("2*A3*ABS(B3)"));
#使用公式计算
s1.write(3,0,'right',xlwt.easyxf("align:horiz right"))
#调整对齐方式
x.save('example.xls') #保存

import os
os.system('explorer example.xls')

以上为xlwt库操作Excel的脚本,直接保存运行可以看效果。几乎可以实现所有常用Excel的操作了。支持的Excel格式为Excel 2003及之前的版本。xlwt库可以使用命令pip install xlwt安装。相应的读取模块为xlrd。或者可以去pip的官网下载。代码里的注释已经很详细了。


下面说你文本文件的操作。其实只需要读取进来,逐行处理。每一行尝试用.split分割,然后加上一个判断语句调整格式,写入Excel相应的行即可。



这个以前都做过几次。用COM接口很麻烦 。现在有了xlwt和xlrd都变得很容易。你略略看一看它们的例子代码就会了。我好象最近从考勤机里读取EXCEL数据。再重新加工后,生成EXCEL的报告,做过一个程序,大约5-7小时吧。

  • 濡備綍鍒╃敤pyrhon鑴氭湰杈撳嚭txt鏂囦欢鍜宑sv鏂囦欢鐨勭浉鍚屾暟鎹,骞跺皢鍏惰緭鍑轰负txt鏂...
    绛旓細浣犲彲浠ヤ娇鐢 Python 鐨鍐呯疆妯″潡鏉ュ疄鐜拌緭鍑虹浉鍚屾暟鎹埌 txt 鏂囦欢鍜 csv 鏂囦欢锛屽苟灏嗗叾杈撳嚭涓 txt 鏂囦欢銆備互涓嬫槸涓涓ず渚嬩唬鐮侊細```python import csv 鍑嗗鏁版嵁 data = [['Name', 'Age', 'City'],['John', '25', 'New York'],['Alice', '30', 'London'],['Bob', '20', 'Paris']]杈撳嚭...
  • 鎬庝箞鐢python璇诲彇txt鏂囦欢閲屾寚瀹氳鐨勫唴瀹,骞跺鍏xcel
    绛旓細涓銆佽excel琛 璇籩xcel瑕佺敤鍒皒lrd妯″潡锛屽畼缃戝畨瑁咃紙http://pypi.python.org/pypi/xlrd锛夈傜劧鍚庡氨鍙互璺熺潃閲岄潰鐨勪緥瀛愮◢寰瘯涓涓嬪氨鐭ラ亾鎬庝箞鐢ㄤ簡銆傚ぇ姒傜殑娴佺▼鏄繖鏍风殑锛1銆佸鍏ユā鍧 澶嶅埗浠g爜浠g爜濡備笅:import xlrd 2銆佹墦寮Excel鏂囦欢璇诲彇鏁版嵁 澶嶅埗浠g爜浠g爜濡備笅:data = xlrd.open_workbook('excel.xls')3銆...
  • python 灏瀵煎叆鐨txt鏂囦欢瀛樺埌dict閲岄潰
    绛旓細-*- coding: UTF-8 -*-__author__ = 'lpe234'''# tmp.txt鏂楅瓊,1鎭堕瓟灏戠埛钀濊帀鎺,0瀹橀,1鍖绘墜閬ぉ,1瀹橀,1鐮寸獚涔濆ぉ,1澶╀笅甯堝厔涓鑸粦,0绀肩墿锛氶亣瑙佷簬鏈﹁儳,0閲嶇敓涔嬮敠缁e濂,1鍏堝鍘氱埍,0鍏堝鍘氱埍,1'''def main(): tmp_dict = {} with open('tmp.txt', 'r') as f: tx...
  • 鐢python濡備綍灏嗘枃浠跺す鍐呴儴鍒嗘寚瀹氭枃浠跺悕鐨勬枃浠跺鍒跺埌鐩爣鏂囦欢澶,澶т浆姹 ...
    绛旓細import glob import shutil def copy_file(names,old_name,new_name):for name in names:filename = name.split("\\")[-1]filename锛氫粠璺緞涓埅鍙鏂囦欢鍚 shutil.copyfile(old_name + filename, new_name + filename)files = glob.glob(r'D:/A/1*.txt')files 锛 鎼滅储寰楀埌鐨勭鍚堟潯浠讹紙甯...
  • python鎬庝箞璇诲彇txt鏂囦欢
    绛旓細鏂规硶涓锛歠 = open("foo.txt") # 杩斿洖涓涓鏂囦欢瀵硅薄 line = f.readline() # 璋冪敤鏂囦欢鐨 readline()鏂规硶 while line: print line, # 鍚庨潰璺 ',' 灏嗗拷鐣ユ崲琛岀 # print(line, end = '')銆# 鍦 Python 3涓娇鐢 line = f.readline() f.close()鏂规硶浜岋細for lin...
  • 濡備綍鎶妏ython鐨缁撴灉瀵煎叆txt鏂囦欢涓幓?
    绛旓細from __future__ import print_function print(filtered_sentence,file="鏂囦欢鍚")
  • 鐢python鎵惧嚭涓涓txt鏂囦欢涓殑閲嶅鏁版嵁,骞跺皢閲嶅鏁版嵁杈撳嚭鎴愬彟涓涓猼xt鏂...
    绛旓細鍋囪浣犵殑鏂囦欢鍚嶆槸a.txt锛屽啓鍒癰.txt d = {}for line in open('a.txt'): d[line] = d.get(line, 0) + 1 fd = open('b.txt', 'w')for k, v in d.items(): if v > 1: fd.write(k)fd.close()
  • 鏈夊嚑鍗冧釜TXT鏍煎紡鐨刲og鏂囦欢,鎬庝箞鐢Python鎵归噺鎻愬彇姣忎釜log鏂囦欢鍥哄畾浣嶇疆鐨...
    绛旓細1.灏嗚繖浜TXT鏂囦欢鍏堝悎骞 2.鎵惧埌杩欎簺鍏蜂綋鏁板肩殑鐗瑰緛 3.鐢ㄦ鍒欒〃杈惧紡杩囨护鎻愬彇 杩欓樀瀛愭瀛︿範python,涓嶄粙鎰忓彲浠ュ彂缁欐垜锛岀粌鐫鐜
  • python鎬庢牱璇诲彇txt鏂囦欢鐨勬暟鎹唴瀹
    绛旓細鎴戜滑浣跨敤python鐨鏃跺欑粡甯搁渶瑕佽鍙txt鏂囦欢涓殑鍐呭锛岄偅涔堣濡備綍璇诲彇鍛紵涓嬮潰鎴戠粰澶у鍒嗕韩涓涓嬨01 棣栧厛鎴戜滑鏂板缓涓涓猼xt鏂囦欢锛屽湪閲岄潰鍐欏叆涓浜涘唴瀹癸紝濡備笅鍥炬墍绀 02
  • python瀵规枃鏈鏂囦欢鐨勮鏈夊摢浜涙柟娉,鍐欐湁鍝簺鏂规硶?
    绛旓細1 鏂囦欢璇诲彇鍏ㄦ枃鏈搷浣 鍦ㄤ竴瀹氬満鏅笅鎴戜滑闇瑕鎶鏂囨湰鍏ㄩ儴鍐呭璇诲彇鍑烘潵锛岃繘琛屽鐞嗐python鎻愪緵涓夌鍑芥暟璇诲彇鏂囦欢锛屽垎鍒槸read readline readlines锛宺ead()锛氳鍙栨枃浠剁殑鍏ㄩ儴鍐呭锛屽姞涓婂弬鏁板彲浠ユ寚瀹氳鍙栫殑瀛楃銆俽eadline()锛氳鍙栨枃浠剁殑涓琛屻俽eadlines()锛氳鍙栨枃浠剁殑鎵鏈夎鍒板唴瀛樹腑銆備笉鍚屽満鏅笅鎴戜滑鍙互閫夋嫨涓嶅悓鍑芥暟瀵...
  • 扩展阅读:pycharm创建txt文件 ... python更改txt文件内容 ... python读取txt文件并处理 ... python数据txt文件 ... python读取桌面txt文件 ... python导入处理txt文件 ... python如何打开文件txt ... python怎么导入txt文件 ... python怎么调用txt文件 ...

    本站交流只代表网友个人观点,与本站立场无关
    欢迎反馈与建议,请联系电邮
    2024© 车视网