Python文本文件串转换为Excel文件中的列

时间:2017-05-07 05:32:39

标签: python excel string windows spreadsheet

我是Python的新手,目前正在为我的工作做这件事。我想问一些新手问题。

我有一个文本文件,其中包含以下格式的行数:

融化,游泳,液化,流动性,流动性,液体,液体状态,流畅,流畅,清澈,流畅

全部以字符串形式出现。

我想以下列格式将这些字符串提取到excel文件中 - 这样每个'set'字符串都在一个单独的列中。

       |        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    |             |                |

我的代码有什么问题..?

1 个答案:

答案 0 :(得分:0)

  

问题:我想以下列格式将这些字符串提取到excel文件中

       |     A       |       B        |     C 
---------------------------------------------------
  1    | melted      |   swimming     |  liquified
一行中

三个单词,但根据您的问题数据,您在一个文字行中有 6个单词

  

无法重现您的输出

1    | melted, swimming, liquified, liquidity, liquidness, liquid

这对我有用:

one_line_of_data = "melted, swimming, liquified, liquidity, liquidness, liquid\n"

for line in f:
    liw = [word.strip() for x,word in enumerate(line.split(","))]
    data.append(liw[:3])
    data.append(liw[3:])

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])

使用Python测试:3.4.2