當前位置:首頁 » 編程語言 » pythondocument

pythondocument

發布時間: 2022-04-16 11:34:55

python用file對象和open方法處理文件的區別

python document 是這么說的:

File objects are implemented using C』s stdio package and can be created
with the built-in open() function. File objects are also returned by
some other built-in functions and methods, such as os.popen() and
os.fdopen() and the makefile()
method of socket objects. Temporary files can be created using the
tempfile mole, and high-level file operations such as ing, moving,
and deleting files and directories can be achieved with the shutil
mole.

並且對File 對象的構造函數說明如下:

file(filename[, mode[, bufsize]])

Constructor function for the file type, described further in section
File Objects. The constructor』s arguments are the same as those of the
open() built-in function described below.

When opening a file, it』s preferable to use open() instead of invoking
this constructor directly. file is more suited to type testing (for
example, writing isinstance(f, file)).

New in version 2.2.

但是其實我在如下代碼段中,def setNodeManagerDomain(domainDir):

try:

domainName = os.path.basename(domainDir)

fd = open(domainDir + '/nodemanager/nodemanager.domains', 'w')

fd.write('#Domains and directories created by Configuration Wizard.\n')

fd.write('#' + time.ctime() + '\n')

dirNorm=os.path.normpath(domainDir).replace('\\','\\\\')

fd.write(domainName + '=' + dirNorm)

print 'create domain file and close in the end under the directory:' + domainDir

fd.close

except Exception, e:

print 'Failed to create domain file in the directory:' + domainDir

我使用file對象 or open方法在windows 環境下都能通過,但是程序部署到linux環境中就出現問題。

[echo] NameError: file

可能linux環境對file支持不好,所以保險起見,還是遵循文檔中所說的,堅持用open方法吧。

㈡ python官方document中關於lambda的一個小問題

pairs.sort(key=lambda pair:pair[2])表示按每個元素的第三個參數排序;parts的每個元素是只有兩個元素元組,因此出現錯誤

㈢ 如何用python讀取word

使用Python的內部方法open()讀取文本文件

try:
f=open('/file','r')
print(f.read())
finally:
iff:
f.close()

如果讀取word文檔推薦使用第三方插件,python-docx 可以在官網上下載

使用方式

#-*-coding:cp936-*-
importdocx
document=docx.Document(文件路徑)
docText=' '.join([
paragraph.text.encode('utf-8')forparagraphindocument.paragraphs
])
printdocText

㈣ 哪位大神知道如何用Python中的docx模塊刪除docx文檔中的表格

可以使用docx模塊。

from docx import Document # 導入庫"""word表格中"""
path = "C:\\Users\\1\\Desktop\\測試.docx" # 文件路徑
document = Document(path) # 讀入文件
tables = document.tables # 獲取文件中的表格集
刪掉指定的那一個表格即可
希望我的回答對你有幫助~

㈤ Python想把兩篇中文文檔的內容作為作為文檔輸入,在Python中用document list表示,應該怎麼寫代碼

withopen('xxx.txt','r')asf:
documents.append(f.read())

中文文檔,還需要注意編碼的問題

㈥ 求助大神:如何用Python docx解析一個Word文檔,在某些欄位處插入文本或表格,更換頁眉頁腳等急~

from docx import Document
from docx.shared import Inches

document = Document()

document.add_heading('Document Title', 0)

p = document.add_paragraph('A plain paragraph having some ')
p.add_run('bold').bold = True
p.add_run(' and some ')
p.add_run('italic.').italic = True

document.add_heading('Heading, level 1', level=1)
document.add_paragraph('Intense quote', style='IntenseQuote')

document.add_paragraph(
'first item in unordered list', style='ListBullet'
)
document.add_paragraph(
'first item in ordered list', style='ListNumber'
)

document.add_picture('monty-truth.png', width=Inches(1.25))

table = document.add_table(rows=1, cols=3)
hdr_cells = table.rows[0].cells
hdr_cells[0].text = 'Qty'
hdr_cells[1].text = 'Id'
hdr_cells[2].text = 'Desc'
for item in recordset:
row_cells = table.add_row().cells
row_cells[0].text = str(item.qty)
row_cells[1].text = str(item.id)
row_cells[2].text = item.desc

document.add_page_break()

document.save('demo.docx')
這是一個demo for docx 你可以試試

㈦ 如何用python查詢文件路勁

最近在用Python腳本處理文件夾下面的文件名的搜索和重命名。其中碰到如何遞歸遍歷文件夾下面所有的文件,找到需要的文件,並且重命名的問題。其實如果看看Python的document,還是比較簡單的,這里直接給出使用方法,免得大家還要花精力去查找。

環境:
文件夾結構:
----path1
----path1-1
----path1-1.1.txt
----path1-2
----path1.1.txt
----path2
----recursiveDir.py
文件夾結構如上所示。

代碼分析(recursiveDir.py):

[python] view plain
<span style="font-size:18px;">import os

'''''
本腳本用來演示如何遍歷py腳本所在文件夾下面所有的文件(包括子文件夾以及其中包含的文件)。
重點演示如何獲取每個文件的絕對路徑。注意os.path.join(dirpath, filename)的用法。
'''

rootdir = os.getcwd()
print('rootdir = ' + rootdir)

for (dirpath, dirnames, filenames) in os.walk(rootdir):
#print('dirpath = ' + dirpath)
for dirname in dirnames:
print('dirname = ' + dirname)
for filename in filenames:
#下面的列印結果類似為:D:\pythonDirDemo\path1\path1-1\path1-1.1.txt
print(os.path.join(dirpath, filename))
if(filename=='path1-1.1.txt'):
os.chdir(dirpath)
#os.rename(os.path.join(dirpath, filename), dirpath + os.sep + 'path1-1.1.new.txt')
os.rename('path1-1.1.txt', 'path1-1.1.new.txt')
#os.remove(os.path.join(dirpath, filename))
#下面的輸出為fileName = path1-1.1.txt,並未包含絕對路徑,所以需要使用os.path.join來鏈接,獲取絕對路徑
print('fileName = ' + filename)
print('------------------one circle end-------------------')</span>

所以可以看到程序中使用os.path.join(dirpath, filename)來拼接出絕對路徑出來。注意下面的重命名用法,可以將工作目錄切換到os.chdir(dirpath),這樣就可以直接用os.rename(oldfile, newfile).Python會自動到dirpath下面查找oldfile並且重命名為newfile。注意工作目錄的含義:在Python的GUI中,使用os.getcwd()可以獲取到當前工作目錄。測試如下:

[html] view plain
<span style="font-size:18px;">>>> os.chdir('D:')
>>> os.getcwd()
'D:\\pythonDirDemo\\path1\\path1-1'
>>> os.chdir('D:\\')
>>> os.getcwd()
'D:\\'</span>

可見卻是可以用chdir改變工作目錄。這個代碼只是在重命名的時候用到的小技巧而已,大家知道有這個東西就行了,不過調用chdir之後,後續再獲取getcwd()就會被影響,所以警惕。

㈧ Python Documentation 是不是沒有中文版

比較古老的Python中文手冊了,版本是2.4,不過Python的語法並沒有顯著的變化

對於了解基礎語法還是足夠的



㈨ python互動式窗口總是彈出documentation,怎麼關閉彈出

設置快捷鍵。

SublimeREPL安裝之後沒有快捷鍵,每次運行程序必須用滑鼠去點工具欄,有些不爽,所以需要給SublimeREPL添加快捷鍵。
點擊首選項->按鍵綁定-用戶,然後在彈出的文件中輸入:

本文僅定義了Python - RUN current file的快捷鍵,我在這里使用的是F5,可以根據自己的需要進行靈活的調整。

為了調試方便,我們可以把這個窗口放到右邊,先設置sublime的窗口顯示為多層,點擊查看->布局->列數 2列
ok!現在可以寫一個測試代碼運行一下,按f5運行,
PS:現在有一個問題,就是每次運行py文件的時候,都會新建一個REPL窗口。如下圖所示:

熱點內容
shell腳本平方計算公式 發布:2024-04-26 23:29:26 瀏覽:186
比較實惠的雲伺服器 發布:2024-04-26 23:24:57 瀏覽:973
怎麼增加電腦緩存 發布:2024-04-26 23:23:46 瀏覽:450
android調試gdb 發布:2024-04-26 23:22:27 瀏覽:98
androidsocket服務 發布:2024-04-26 22:49:53 瀏覽:980
python編譯時加密 發布:2024-04-26 22:49:20 瀏覽:246
買車看哪些配置參數 發布:2024-04-26 22:45:50 瀏覽:835
linux顯示圖像 發布:2024-04-26 22:45:41 瀏覽:493
flash腳本格式 發布:2024-04-26 22:43:41 瀏覽:452
c語言求三位數 發布:2024-04-26 22:43:39 瀏覽:690