Python 把txt檔案轉換成csv

2022-07-12 09:27:11 字數 3125 閱讀 8438

最近在專案上需要批量把txt檔案轉成成csv檔案格式,以前是手動開啟excel檔案,然後匯入txt來生產csv檔案,由於這已經變成每週需要做的事情,決定用python自動化指令碼來實現,思路:

讀取資料夾中所有txt檔案,儲存到list中

針對每個txt檔案,自動生產同檔名的csv檔案

對每個txt檔案,根據分隔符來儲存為csv檔案,分隔符為分號「;」,在轉換之前先把檔案編碼統一成'utf-8',因為在實現過程中,發現總會有編碼報錯問題出現

新建txt資料夾來存放所有txt檔案

完整**如下:

import

csvimport

osimport

shutil

from chardet.universaldetector import

universaldetector

defget_encode_info(file):

with open(file, 'rb

') as f:

detector =universaldetector()

for line in

f.readlines():

detector.feed(line)

ifdetector.done:

break

detector.close()

return detector.result['

encoding']

defread_file(file):

with open(file, 'rb

') as f:

return

f.read()

defwrite_file(content, file):

with open(file, 'wb

') as f:

f.write(content)

defconvert_encode2utf8(file, original_encode, des_encode):

file_content =read_file(file)

file_decode = file_content.decode(original_encode,'

ignore')

file_encode =file_decode.encode(des_encode)

write_file(file_encode, file)

## move *.txt to a folder

defmove2txtfolder(path, txt_file_list):

txt_folder_path = path + '

\\txt'if

notos.path.exists(txt_folder_path):

os.makedirs(txt_folder_path)

for file in

txt_file_list:

des_path =os.path.join(txt_folder_path, os.path.basename(file))

shutil.move(file, des_path)

##在路徑中找出所有的*.txt檔案

deffindtxt(path, txt_file_list):

file_name_list =os.listdir(path)

for filename in

file_name_list:

de_path =os.path.join(path, filename)

ifos.path.isfile(de_path):

if de_path.endswith("

.txt

"): #

specify to find the txt file.

else

: findtxt(de_path, txt_file_list)

deftxt2csv(txt_file):

##先把所有檔案的encoding都轉換成utf-8

encode_info =get_encode_info(txt_file)

if encode_info != '

utf-8':

convert_encode2utf8(txt_file, encode_info,

'utf-8')

csv_file = os.path.splitext(txt_file)[0] + '

.csv

'with open(csv_file, 'w+

', newline='', encoding='

utf-8

') as csvfile:

writer = csv.writer(csvfile, dialect='

excel')

with open(txt_file, 'r

', encoding='

utf-8

') as txtfile:

for line in

txtfile.readlines():

line_list = line.strip('

\n').split(';'

) writer.writerow(line_list)

if__name__ == '

__main__':

folder_path = r'

c:\details'#

##如果資料夾中還有子資料夾,請用findtxt函式

#txt_file_list =

#findtxt(folder_path, txt_file_list)

##如果資料夾中沒有子資料夾的時候直接使用推導式來生產txt檔案的list

txt_file_list = [os.path.join(folder_path, file) for file in os.listdir(folder_path) if os.path.join(folder_path, file).endswith('

.txt')]

for txt_file in

txt_file_list:

txt2csv(txt_file)

move2txtfolder(folder_path, txt_file_list)

如何把PDF檔案內容轉換成TXT

pdf格式文件的興起使得現在很多文件都用pdf格式進行傳遞一些文件資料內容,如果拿到這種格式的文件,需要用到裡面的一些文字內容,有些人就會進行複製貼上了,但是如果需要的文字內容比較多,顯然一頁一頁的複製操作是很浪費時間的,如果要將pdf文字內容提取出來可以直接將檔案轉換為txt格式就可以了。一般我們...

python將txt檔案轉換成csv

直接上 coding utf 8 import pandas as pd import configparser import csv from py2neo import graph,node,relationship import urllib3 urllib3.disable warnings...

將Txt檔案轉換成dataset

今天工作中,遇到需要將txt檔案轉化到記憶體表dataset中,於是寫了乙個方法來實現 txt檔案是特定格式的,檔案中,一條記錄為一行,各欄位之間用 分割 注 最後乙個欄位後,有 各欄位依次為資料庫中的相應字段。舉例如下 id號 線路編碼 車站 編碼 執行模式 模式設定日期 模式設定時間 1 98 ...