如何将整个表格从文本转换为整数

发布于 2024-10-16 07:26:15 字数 334 浏览 2 评论 0原文

我有一个读取的 txt 文件:

fdata =[]
with open(fn,'rb') as fo:
     for i in xrange(1):
           fo.next()
     for line in fo:                       
           data = line.split(',')
           data = data[:23]
           fdata.append(data)    
     print fdata 

我想将整个表从字符串转换为数字。 AND 将表大小限制为读取 XXX 行。

I have a txt file that I read with:

fdata =[]
with open(fn,'rb') as fo:
     for i in xrange(1):
           fo.next()
     for line in fo:                       
           data = line.split(',')
           data = data[:23]
           fdata.append(data)    
     print fdata 

I would like convert the entire table to numbers from string. AND limit the table size to read to XXX lines.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独行侠 2024-10-23 07:26:15

要限制行数,请尝试使用计数器和 readlines() 进行迭代:

maxlines = 100
lines = f.readlines()

if maxlines > len(lines):  maxlines = len(lines)

for i in xrange(maxlines):
  line = lines[i]

  # Do your stuff...

将字符串转换为数字(在本例中为整数。float() 用于浮点数) :

print '9000' == 9000 # False.
print int('9000') == 9000 # True

因此,要使您的表仅包含整数,请将每一行(拆分后)转换为整数。 int()返回一个整数,因此它不会修改您的变量。将 fdata.append(data)) 替换为 fdata.append(int(data()) 应该可以解决问题。


好的,要将整个列表转换为整数,只需迭代即可将这 23 个元素中的每一个都转换为整数,这适用于任意字符串列表:

for element in data:
  element = int(element)

要将列表转换为整数,但要排除第一个元素,请循环遍历它:

for i in xrange(1, len(data)): # I used 1 because 0 is the first element. This skips it.
  data[i] = int(data[i])

To limit lines, try iterating using a counter and readlines():

maxlines = 100
lines = f.readlines()

if maxlines > len(lines):  maxlines = len(lines)

for i in xrange(maxlines):
  line = lines[i]

  # Do your stuff...

To convert a string to a number (an integer in this case. float() is for floating points):

print '9000' == 9000 # False.
print int('9000') == 9000 # True

So to make your table only include integers, convert each line (after splitting) into an integer. int() only returns an integer, so it doesn't modify your variable. Replacing fdata.append(data)) with fdata.append(int(data() should do the trick.


Okay, to convert your entire list into integers, just iterate over each of those 23 elements to convert them into integers. This works for an arbitrary list of strings:

for element in data:
  element = int(element)

To convert your list into integers, but to exclude your first element, loop through it:

for i in xrange(1, len(data)): # I used 1 because 0 is the first element. This skips it.
  data[i] = int(data[i])
吃素的狼 2024-10-23 07:26:15
from itertool import islice
fdata =[]
with open(fn,'rb') as fo:
     for i in xrange(1):
           fo.next()
     for line in islice(fo,0,XXX):                       
           data = line.split(',')
           data = data[:23]
           fdata.append(map(int,data))    
     print fdata 
from itertool import islice
fdata =[]
with open(fn,'rb') as fo:
     for i in xrange(1):
           fo.next()
     for line in islice(fo,0,XXX):                       
           data = line.split(',')
           data = data[:23]
           fdata.append(map(int,data))    
     print fdata 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文