【问题标题】:python replace word as per conditionpython根据条件替换单词
【发布时间】:2013-12-10 02:12:58
【问题描述】:

在标准输入中,我提供以下文件:

    #123     595739778       "neutral"       Won the match #getin
    #164     595730008       "neutral"      Good girl

数据#2 如下所示:

    labels 1 0 -1
    -1 0.272653 0.139626 0.587721
    1 0.0977782 0.0748234 0.827398

我想看看它在 data#2 文件中的 -1 是否替换为负数,1 然后是正数,0 然后是中性

以下是我的问题:

  1. 从第 2 行的 data#2 文件开始
  2. 我遇到了更换问题。我想像下面那样替换,但它显示一个错误,它需要 1 个参数,但我已经有 2 个参数。
  3. 如果我这样做,如下所示(注意打印语句):

    if binary == "-1":
      senti = str.replace(senti.strip('"'),"negative")
    elif binary == "1":
      senti = str.replace(senti.strip('"'),"positive")
    elif binary == "0":
      senti = str.replace(senti.strip('"'),"neutral")
    print id, "\t", num, "\t", senti, "\t", sent
    

    但如果我这样做(注意打印),那么它不会进入“如果条件”:

    if binary == "-1":
       senti = str.replace(senti.strip('"'),"negative")
    elif binary == "1":
       senti = str.replace(senti.strip('"'),"positive")
    elif binary == "0":
       senti = str.replace(senti.strip('"'),"neutral")
    

    打印id, "\t", num, "\t", senti, "\t", 发送

那我该如何打印。 我得到的输出: #123 595739778 “中立”赢得比赛#getin #164 595730008 “中性”好女孩

 output expected (replace just replaces the negative, positive & neutral as per data# file:

    #123     595739778       negative       Won the match #getin
    #164     595730008       positive       Good girl

错误:

 Traceback (most recent call last):
   File "./combine.py", line 17, in <module>
     senti = str.replace(senti.strip('"'),"negative")
 TypeError: replace() takes at least 2 arguments (1 given)

这是我的代码:

for line in sys.stdin:
    (id,num,senti,sent) = re.split("\t+",line.strip())
    tweet = re.split("\s+", sent.strip().lower())
    f = open("data#2.txt","r")
    for line1 in f:
       (binary,rest,rest1,test2) = re.split("\s", line1.strip())
       if binary == "-1":
          senti = str.replace(senti.strip('"'),"negative")
       elif binary == "1":
          senti = str.replace(senti.strip('"'),"positive")
       elif binary == "0":
          senti = str.replace(senti.strip('"'),"neutral")
       print id, "\t", num, "\t", senti, "\t", sent

【问题讨论】:

  • 你能发布你得到的错误吗?
  • @qmorgan 检查我的编辑

标签: python regex file file-io


【解决方案1】:

您实际上缺少替换的参数;因为它是字符串本身的一个方法,你可以这样做:

In [72]: str.replace('one','o','1')
Out[72]: '1ne'

In [73]: 'one'.replace('o','1')
Out[73]: '1ne'

在您的代码中,您可能想要,例如

   if binary == "-1":
      senti = senti.strip('"').replace("-1","negative")

要跳过 data#2 文件的第一行,一个选项是

f = open("data#2.txt","r")
for line1 in f.readlines()[1:]: # skip the first line
   #rest of your code here

编辑:聊天对话后,我认为您想要的更像是以下内容:

f = open("data#2.txt","r")
datalines = f.readlines()[1:]

count = 0

for line in sys.stdin:
    if count == len(datalines): break # kill the loop if we've reached the end
    (tweetid,num,senti,tweets) = re.split("\t+",line.strip())
    tweet = re.split("\s+", tweets.strip().lower())
    # grab the right index from our list
    (binary,rest,rest1,test2) = re.split("\s", datalines[count].strip())
    if binary == "-1":
        sentiment = "negative"
    elif binary == "1":
        sentiment = "positive"
    elif binary == "0":
        sentiment = "neutral"
    print tweetid, "\t", num, "\t", sentiment, "\t", tweets
    count += 1 # add to our counter

【讨论】:

  • 我听不懂你在这里想说什么。你能改写一下吗?
  • 你得到了什么输出,你期望什么?
  • 再次,您发布了您期望的输出,但不是您实际得到的输出。如果您没有提供足够的信息,我无能为力。
猜你喜欢
  • 1970-01-01
  • 2020-09-15
  • 2022-01-19
  • 2019-05-20
  • 2023-02-13
  • 2020-03-27
  • 2020-07-05
  • 1970-01-01
  • 2016-08-29
相关资源
最近更新 更多