【443】Tweets Analysis Q&A

孤街醉人 提交于 2019-12-01 14:52:07
  1. 【Question】
    When converting Tweets info to csv file, commas in the middle of data (i.e. location: Sydney, NSW) can make a mistake of the csv file (creaing more columns).
    【Solution】
    The solution is to add double quotation marks on both sides of the content, like this:
    fo.write("\"" + str(tweet["user"]["location"]) + "\"")
  2. 【Question】
    When open csv file with Excel, sometimes it will show messy code, but it can show well with Notepad.
    ref:csv 文件打开乱码,有哪些方法可以解决?
    【Solution】
    One solution is opening this file with notepad++.
    Another solution is adding codes at the beginning of the writing file, like this:
    fo = open(r"D:\Twitter Data\Data\test\tweets.csv", "w")
    fo.write("\ufeff")
  3. 【Question】
    Text contents contain carriage return, double quotation marks, single quotation marks. Those info will make mistakes when creating csv file. 
    【Solution】
    So we should replace those characters with space or nothing, like this:
    text = str(tweet["text"])
    text = text.replace("\n", " ")
    text = text.replace("\"", "")
    text = text.replace("\'", "")
    fo.write("\"" + text + "\"")
    Including tweet["user"]["location"] and tweet["text"], for these two attributes, user can write whatever they want, so it's easy to make mistakes.

  4. 【Question】
    After converting Tweets to csv file, but I can't open this file by pandas.read_csv(). The reason is there must be some problems in those data. Since there are about more than 100000+ rows of this csv file, how can I locate the error line?
    【Solution】
    Solution is coverting the first 10000 rows, if there are not errors, and then converting the next 10000 rows. If error occurs, trying to narrow the range of numbers, like error occurs between 20000 to 30000, we can change the range of numbers with 20000 to 25000. Using this method several times, we can locate the error line and find the real problems. For this spicific case, most problems are about contents include carriage return, double quotation marks, etc.
    Codes like this:
    ...
    
    count = 0
    or line in tweets_file:
        try:
            count += 1
            if (count < 10000):
                continue
            ...
    
            if (count > 20000):
                break
        except:
            continue
    ...

 

 

 

 

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!