我有一個看起來像這樣的 txt 檔案:
category test_1
aaa.com; test info - tw
bbb.com; test info - al
category test_2
ccc.com; test info - al
ddd.com; test info - tw
eee.com; test info - tw
category test_3
fff.com; test info - tw
ggg.com; test info - al
hhh.com; test info - tw
iii.com; test info - al
我需要幫助創建一個 Python 腳本,該腳本提取 txt 檔案的一部分并將其匯出到 excel 檔案。例如,如果我想匯出類別“test_1”中的條目,腳本將在 excel 檔案中生成以下輸出。
| A. | B. | C. |
---------------------------------------
1. | aaa.com | test info - tw | |
---------------------------------------
2. | bbb.com | test info - al | |
---------------------------------------
3. | | | |
我嘗試使用下面的代碼
我的 txt 檔案作為 autotest.txt 保存在我的桌面上
import pandas as pd
df = pd.read_csv(‘C:\Users\A12345\Desktop\autotest.txt’)
df.to_excel(‘output.xlsx’, ‘Sheet1’)
當我運行此代碼時,它不會創建 Excel 檔案。我還嘗試在我的桌面上添加一個名為“output.xlsx”的excel檔案,當我運行腳本時,它也沒有將文本添加到excel檔案中。
uj5u.com熱心網友回復:
可以將這種獨特的格式轉換為以“類別”為關鍵字的 csv
file=open("text_file.txt",'r')
data=file.read()
categories=data.split('category')#One approach, if a 'category' string is present
dict_format={}
for categor_data in categories:
items=categor_data.split('\n') #split to lines
dict_format[items[0].replace(" ", "")]=items[1:]#removes spaces from name of categories
for name in dict_format:
print(name)
print("which category to export to.csv format?")
answer=input()
with open(answer ".csv",'w') as csv:
for row in dict_format[answer][:-1]:
if row != "": #if not empty.
csv.write(row.replace(";",",") "\n")
csv.write(dict_format[answer][-1].replace(";",","))
csv.close()
#Now you should be able convert that csv file to xlsx using pandas
控制臺視窗:
>>>run.py
test_1
test_2
test_3
which category to export to.csv format?
test_1
>>>
test_1 .csv 檔案看起來像文本格式:
aaa.com, test info - tw
bbb.com, test info - al
uj5u.com熱心網友回復:
我使用了模塊XlsxWriter;你可以用pip3 install XlsxWriter. 我撰寫的代碼按預期作業:
import xlsxwriter
# this is used to filter. The code expect for the category num, such as 1, 2 or 3
num = input('Give me category number: ')
# you can do checks here if input should be something different
num = int(num)
start_portion_line = 'category test_{}'.format(num)
end_portion_line = 'category test_{}'.format(num 1)
start_index = 0
end_index = 0
with open('path/to/your/txt/file', 'r') as f:
lines = f.readlines()
# find indexes that define the wanted portion
for i,line in zip(range(len(lines)), lines):
if line.strip() == start_portion_line:
start_index = i
elif line.strip() == end_portion_line:
end_index = i - 1
if end_index == 0:
end_index = len(lines)
# getting only the wanted lines
lines = lines[start_index:end_index]
# removing blank lines
while '\n' in lines:
lines.remove('\n')
workbook = xlsxwriter.Workbook('output.xlsx')
worksheet = workbook.add_worksheet()
for i,line in zip(range(len(lines)), lines):
# removing initial spaces
line = line.strip()
# separating tokens
columns = line.split(';')
# writing
for col,j in zip(columns, range(len(columns))):
worksheet.write(i, j, col)
workbook.close()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/410258.html
標籤:
下一篇:Excel中的價格中斷公式
