首页 \ 问答 \ 无法打印已转换为字符串的整数(Can't print an integer that has been converted to a string)

无法打印已转换为字符串的整数(Can't print an integer that has been converted to a string)

我正在尝试做一些应该很简单的事情。 我有点沮丧,为什么我的代码无法正常工作,所以任何帮助表示赞赏:)。

我的程序读入数据,然后创建一个记录特定项目出现次数的字典(我正在使用Twitter数据并计算hashtag出现次数)。 我想输出最热门的推文,并找到了一个很好的简单方法来实现这一点,使用以下内容:

def main():
    tweet_file = open(sys.argv[1])
    tweet_dic = lines(tweet_file) #function that makes my dictionary
    for i in range(0,10):
        big_key = max(tweet_dic, key = lambda i: tweet_dic[i])
        big_value = tweet_dic[big_key]
        sys.stdout = big_key + " " + str(big_value)
        del tweet_dic["big_key"]
    tweet_file.close()

我得到的错误是AttributeError: 'str' object has no attribute 'write'

现在我已经使用print输出了两个不同的值到终端使用print就好了,它们可以放在两个不同的print语句,没有问题,因为我不必连接或任何东西。 我已经检查了两个变量类型,并且如预期的那样,它们始终是strint

我对str (整数转换)函数的理解是,你应该能够传递一个整数并获得一个字符串表示! 转换完成后,我可以在过去没有任何问题的情况下打印出类似的内容。

需要考虑的事情可能会被抛出 - big_key有时可能是一个string ,它通过.encode('utf-8')从Unicode转换而来。 否则,我的文件输出(单独打印)如下所示:

MTVHottest 60

KCAMexico 38

EXO 26

CD9 24

Unicode 19

Coders 18

AlonsoVillapandoTrendy 17

Unicode 14

Unicode 14

有任何想法吗? 提前致谢!


I am trying to do something that should be very simple. I am a bit frustrated for why my code won't work, so any help is appreciated :).

My program reads in data, and then makes a dictionary that records the number of times a particular item occurs (I am using Twitter data and counting hashtag occurrences). I want to output the top tweets, and have found a nice easy way to do that using the following:

def main():
    tweet_file = open(sys.argv[1])
    tweet_dic = lines(tweet_file) #function that makes my dictionary
    for i in range(0,10):
        big_key = max(tweet_dic, key = lambda i: tweet_dic[i])
        big_value = tweet_dic[big_key]
        sys.stdout = big_key + " " + str(big_value)
        del tweet_dic["big_key"]
    tweet_file.close()

The error I get on using this is AttributeError: 'str' object has no attribute 'write'

Now I have outputted the two different values into terminal using print just fine, they can be put in two different print statements with no problems since I don't have to concatenate or anything. I have checked the two variables types, and as expected they are always str & int.

My understanding of the str(integer to convert) function is that you should be able to pass in an integer and get a string representation back! After it has been converted, I have been able to print out things like this in the past with no issues.

Things to consider that may be throwing it out - the big_key can sometimes be a string that was converted from Unicode by .encode('utf-8'). Otherwise the output from my file (printing on separate lines) looks like:

MTVHottest 60

KCAMexico 38

EXO 26

CD9 24

Unicode 19

Coders 18

AlonsoVillapandoTrendy 17

Unicode 14

Unicode 14

Any ideas? Thanks in advance!


原文:https://stackoverflow.com/questions/45552512
更新时间:2024-04-16 12:04

相关问答

更多

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)