[英]Printing a list of integers from a dictionary in python
I have a csv file that holds information that I am feeding to a python script.我有一个 csv 文件,其中包含我提供给 python 脚本的信息。 The list stored in the csv looks similar to
['1','2','3','4','5']
.存储在 csv 中的列表类似于
['1','2','3','4','5']
。 When I create a loop to print out the contents of the list, I get:当我创建一个循环来打印列表的内容时,我得到:
'(new line)
1(new line)
'(new line)
'(new line)
2(new line)
'(new line)
'(new line)
3(new line)
'(new line)
.. until it reaches the end. ..直到它到达终点。 How can I extract the numerical contents of the list without the parenthesis and brackets?
如何在没有括号和方括号的情况下提取列表的数字内容? I tried the
.replace()
but when I have numbers higher than 10, it prints out 1 then 0 as if they were two separate values.我尝试了
.replace()
但是当我的数字大于 10 时,它会打印出 1 然后 0 ,就好像它们是两个单独的值一样。
It seems like you list is not stored in proper.csv format.您的列表似乎未以正确的.csv 格式存储。 When saving a list of items in a csv file, each of the items should be separated by a single comma.
在 csv 文件中保存项目列表时,每个项目都应该用一个逗号分隔。 Any other characters will be considered part of the item itself.
任何其他字符都将被视为项目本身的一部分。
In your case when you save ['1','2','3','4','5']
as a csv, the first item becomes ['1'
the second item becomes '2'
and so on.在您的情况下,当您将
['1','2','3','4','5']
保存为 csv 时,第一项变为['1'
第二项变为'2'
,依此类推。 Try saving the text 1,2,3,4,5
to your csv file.尝试将文本
1,2,3,4,5
保存到 csv 文件中。
Provided you have a list something like this as a list from the CSV
file:如果您有一个类似这样的列表作为
CSV
文件中的列表:
list_example = ["1","2","3","4"]
And, you would like to retrieve the items of the list as an integer, you could do the following:而且,您想将列表中的项目检索为 integer,您可以执行以下操作:
for i in list_example:
print(int(i))
This will give you the result as below in integer "int"
type:这将在 integer
"int"
类型中为您提供如下结果:
>>1
>>2
>>3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.