當前位置:首頁 » 編程軟體 » txt去空格腳本

txt去空格腳本

發布時間: 2024-10-24 22:11:57

❶ 求個批量刪除文件內容空格的腳本

import os
def remove_first_line_and_spaces(file_path):
with open(file_path, 'r') as file:
# 讀取文件的第二行開始的所改含旦有內容
content = file.readlines()[1:]
# 刪除所有行的前導空格和尾隨空格
content = [line.strip() for line in content]
# 將新內容寫迴文件
with open(file_path, 'w') as file:
file.write('\n'.join(content))
def process_directory(directory_path):
for filename in os.listdir(directory_path):
# 構建完整的文件核擾路徑
file_path = os.path.join(directory_path, filename)
if os.path.isfile(file_path):
remove_first_line_and_spaces(file_path)
if __name__ == '__main__':
directory_path = 'path/to/directory'
process_directory(directory_path)
注意:請把 'path/to/directory' 替換為你要處理的目錄的實際路徑老哪。

❷ 如何刪除一個文件夾中所有txt文件中的空格

@echo off
setlocal EnableDelayedExpansion
for /f "delims=" %%a in ('dir /a-d/s/b *.txt') do (
set file=%%~na
set file=!file: =!
ren "%%~fna" "!file!%%~xa"
)
pause
以上代碼是去除文件名中的空格,文件名不變,請採納,謝謝!

❸ 寫bat批處理文件時如何刪除一個文本文件的所有空格謝了

@echooff&
(for/f"delims="%%ain(test.txt)do(
set"line=%%a"
set"line=!line:=!"
echo!line!
))>new.txt

原始文本文件名稱為test.txt,刪除空格後生成的新文本文件為new.txt

❹ 用按鍵精靈刪除txt中的空行和空格,代碼怎麼寫

您好,暫時沒有,個人認為是寫入數據時方法不對,正確的方法應該是

Dim 內容 = Array("1","2","2","2","2","2","2","2","2","2","2","2","2","2","2")

換行寫入txt文件("txtx","sss",內容)

Function 換行寫入txt文件(文件夾名,文件名,內容)

Import "cgc.lua"

Dim 文件路徑= GetSdcardDir()&"/"&文件夾名&"/"

Dim txt文件 = 文件路徑&文件名&".txt"

If Dir.Exist(文件路徑) = 0 Then

Call Dir.Create(文件路徑)// 創建文件夾

End If

If Dir.Exist(txt文件) = 0 Then

Call cgc.CreateFile(txt文件)// 創建文件

End If

For i = 1 To UBound(內容)+1

If i = UBOUND(內容) + 1 Then

Call File.Append(txt文件, 內容(i)) //Chr(13)

Else

Call File.Append(txt文件, 內容(i)&" ") //Chr(13)

End If

Next

End Function

熱點內容
伺服器雲主機改成雲電腦 發布:2025-03-18 02:46:11 瀏覽:422
php環境的搭建 發布:2025-03-18 02:44:47 瀏覽:489
java實現文件上傳到ftp 發布:2025-03-18 02:43:25 瀏覽:401
編程出遊戲 發布:2025-03-18 02:43:15 瀏覽:178
使用公網ip搭建伺服器 發布:2025-03-18 02:34:23 瀏覽:215
android從程序員到架構師之路 發布:2025-03-18 02:32:52 瀏覽:298
高壓存儲罐 發布:2025-03-18 02:23:18 瀏覽:760
加密卡怎麼模擬 發布:2025-03-18 02:02:08 瀏覽:271
我的世界伺服器水桶搭建 發布:2025-03-18 02:01:21 瀏覽:334
微信存儲到sd卡 發布:2025-03-18 01:34:29 瀏覽:969