0
我對Python真的很陌生,目前正在爲我的工作做到這一點。我想問一些新手問題。將Python文本文件字符串轉換爲Excel文件中的列
我有以下這樣的格式將數字的行的文本文件:
熔化,游泳,液化,流動性,liquidness,液體,liquid_state,平滑,流暢,清晰,流體
所有形式的字符串。
我想按照以下格式將這些字符串提取到excel文件中 - 這樣每個字符串的「集合」都在一個單獨的列中。
| A | B | C
------------------------------------------------------
1 | melted | swimming | liquified
------------------------------------------------------
2 | liquidity | liquidness | liquid
我的代碼如下:
import pandas as pd
from nltk.corpus import wordnet
import xlwt
import xlrd
synonyms = []
for syn in wordnet.synsets('liquid'):
for l in syn.lemmas():
synonyms.append(l.name())
A = set(synonyms)
A = list(A)
A = ', ' .join(str(e) for e in A)
#print(A)
with open('Output.txt', 'w') as file:
print(A)
file.write(A)
file.close()
data = []
with open("Output.txt") as f:
for line in f:
data.append([word for word in line.split(" ") if word])
print(data)
wb = xlwt.Workbook()
sheet = wb.add_sheet("New Sheet")
for row_index in range(len(data)):
for col_index in range(len(data[row_index])):
sheet.write(row_index, col_index, data[row_index][col_index])
wb.save("newSheet.xls")
但是當我運行我的代碼,並不如預期的輸出。它輸出爲:
| A | B | C
------------------------------------------------------
1 | melted, swimming, liquified, liquidity, liquidness, liquid
------------------------------------------------------
2 | | |
我的代碼有什麼問題..?