我有一个包含大量空格和冒号的数字文件,我正在尝试删除它们。正如我在这个论坛上看到的那样,函数line.strip.split()
可以很好地实现这一点。有没有办法一次性去除白色空间和冒号?使用Lorenzo发布的方法,我有:
train = []
with open('C:/Users/Morgan Weiss/Desktop/STA5635/DataSets/dexter/dexter_train.data') as train_data:
train.append(train_data.read().replace(' ','').replace(':',''))
size_of_train = np.shape(train)
for i in range(size_of_train[0]):
for j in range(size_of_train[1]):
train[i][j] = int(train[i][j])
print(train)
虽然我收到了这个错误:
File "C:/Users/Morgan Weiss/Desktop/STA5635/Homework/Homework_1/HW1_Dexter.py", line 11, in <module>
for j in range(size_of_train[1]):
IndexError: tuple index out of range
答案 0 :(得分:1)
我认为上面的语法不正确,但无论如何,根据你的问题,你可以使用python中的替换函数。
当从该文件中读取每一行作为字符串时,您可以执行类似的操作,
train = []
with open('/Users/sushant.moon/Downloads/dexter_train.data') as f:
list = f.read().split()
for x in list:
data = x.split(':')
train.append([int(data[0]),int(data[1])])
# this part becomes redundant as i have already converted str to int before i append data to train
size_of_train = np.shape(train)
for i in range(size_of_train[0]):
for j in range(size_of_train[1]):
train[i][j] = int(train[i][j])
这里我使用replace函数用空格字符串替换空格,与冒号类似。
答案 1 :(得分:0)
您没有提供输入文件的示例,因此我们只能推测您需要的解决方案。我想你需要从输入文本文件中提取整数并打印它们的值。
这是我将如何做到的:
以下是它的样子:
import re
input_filename = "/home/evens/Temporaire/Stack Exchange/StackOverflow/Input_file-39359816.txt"
matcher = re.compile(r"\d+")
with open(input_filename) as input_file:
for line in input_file:
for digits_found in matcher.finditer(line):
number_in_string_form = digits_found.group()
number = int(number_in_string_form)
print(number)
但是在你逃避这段代码之前,你应该继续学习Python,因为你似乎还没有掌握它的基本元素。