如何创建一串复合弦 [英] How to create an rray of composites strings

查看:75
本文介绍了如何创建一串复合弦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好:我收集了50个字符串,这些字符串代表文本文件中的注释,其中每一行代表具有不同句子的单独注释。



每个字符串都是产品的用户评论,每个字符串或评论都有几个句子。



如何使用Python使用这些字符串创建数组?



评论示例:
评论1.我对产品感到失望。质量太差了,价格也不合理。我一生中再也不会买这个品牌。



评论2。我对这款产品非常满意。这就是我想要的。感谢品牌J
....到目前为止。



所需结果:


  1. [('失望','产品'),('质量','确实','糟糕','价格','公平')]


  2. [('happy','product'),('all','desired'),('thanks','brand')]


  3. ...

我希望每个处理的注释都放在单独的行中,以将其保存在csv文件中

解决方案

我的第一个项目与此类似,我相信您在寻求以文本文件行作为数组的方式寻求帮助。 / p>

打开文件进行读取:



file = open( / path / to / file, r)



然后我将空格分开:

 用于文件中的行:
打印line.split('')

这将为您提供txt文件中每一行的单词数组
然后在python中使用 csv 模块输入到csv文件中。



引用:




Hi everyone: I have a collection of 50 strings that represent comments in a text file where each line represents a separate comment with different sentences .

Each string is a user review of a product, and each string or review has several several sentences.

How could I to create an array with these strings using Python?

Example of reviews: Review 1. "I was disapointed with the product. The quality was reality bad and the price was not fair. Never in my life I'll buy again this brand.

Review 2. "I'm really happy with this product. It's all what i desired. Thanks brand J" .... and so far.

Desired Results:

  1. [('disappointed', 'product'), ('quality', 'really', 'bad', 'price', 'fair')]

  2. [('happy', 'product'), ('all', 'desired'), ('thanks', 'brand')]

  3. ...

I want each processed comment in a separate line to save it in a csv file

解决方案

One of my first projects was similar to this. I believe your asking for help in getting the text file lines as an array.

To open the file for reading:

file = open("/path/to/file", "r")

I would then split on spaces so:

for line in file: 
      print line.split(' ')

this would give you an array of words from each line in the txt file. Then use the csv module in python to input into a csv file.

References:

这篇关于如何创建一串复合弦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆