如何修复数据的表示 [英] How do I fix the representation of my data

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

问题描述

我想将文本文件中的每三行合并为一行。然而,它有效,我在段落论坛中获得所有新行,而不是逐行列表。我想改变这个:



I wanted to combine every three lines in a text file and make it one line. It worked, however, I get all the new lines in a paragraph forum, not a list as in line by line. I wanted to change this:

T	2009-06-26 16:20:35
U	http://twitter.com/mujiang
W	No Post Title





使它看起来像这样:





to make it look like this:

2009-06-26 16:20:35 http://twitter.com/mujiang No Post Title





我发现唯一的方法是将输出写在一个单独的文件中。这是不可取的,但它是我所知道的唯一方式。



我写了这段代码:



I found that the only way to do that is by writing the output in a separate file. which is unpreferable but its the only way I know.

I wrote this code:

with open('tweets2009-10.txt', "r") as infile:
    for line in infile:
        if 'Apple'in line or 'apple' in line or 'Obama' in line or 'obama' in line:
            fout = open('newdata.txt', 'w')

            line_order = {'T': 'U', 'U': 'W', 'W': 'T'}

            with open('tweets2009-10.txt') as fin:
              prev_head = None
              new_line = ""
              for line in fin:
                cur_head = line[0]
                if prev_head is None or cur_head == line_order.get(prev_head):
                  new_line += line.strip()[1:]
                  if cur_head == 'W':
                    new_line += "\n"
                    fout.write(new_line)
                    new_line = ""
                  else:
                    new_line += ","
                  prev_head = cur_head
                else:
                  pass 
)





但是,新文件new.txt。没有写在那里。我已经运行了5分钟的代码并且它仍然是空的



我不知道代码中可能出现的问题]



请帮忙吗?



我尝试过的事情:



更改方法



However, the new file new.txt. has nothing being written there. I have run the code for 5 minutes and it's still empty

I don't know what might be wrong in the code]

Any help, please?

What I have tried:

changing the method

推荐答案

您打开了两次相同的文件。第二个是循环。

你有条件。根据你的数据,只有U行或W行可以有你的if条件。



下一期是:

如果你的if条件怎么办?匹配,用 w 打开文件名 newdata.txt ,换句话说,对于每个匹配,它将创建一个空文件,销毁前一个文件。即如果您的匹配匹配,即使有多个匹配,您也只能获得一个输出。但是,根据你的下一个命令,这也不会发生。



下一页>

您正在阅读 tweets2009-10.txt 作为fin。您在上部循环中读取了相同的文件 tweets2009-10.txt



我会在这里停下来。



你的整个逻辑是错误的。



建议的逻辑是:

You opened the same file twice. The second one is under a loop.
You have a condition. According your data, only line U or line W can have your if condition.

Next issue is:
What happen if your if condition matched, You open a file name newdata.txt with w, in other word for every match it will crate an empty file destroying the previous file. i.e. If your match matches and even if there is more than one match, you will get only one output. But, according to your next commands, that's not gonna happen either.

Next>
You are reading tweets2009-10.txt as fin. You read the same file tweets2009-10.txt in the upper loop.

I will stop here.

Your whole logic is wrong.

The suggested logic would be:
open output to write
while(more lines in file) 
  read next 3 lines;
  if line 2 or line 3 contains preferred words; then
    format 3 lines as a single line;
    write single line to output file


这篇关于如何修复数据的表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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