抱歉,我是一個完整的初學者,我在 Google 上找不到很多資訊。
對于我的作業,我需要讀取我在檔案中生成的內容,并且只輸出大于 20 但小于 50 的值。我生成了檔案,當我讀取檔案并列印所有數字的內容時在串列中顯示為單個字串值:
['11,24,62,59,1,28,61,5,54,38,31']
我洗掉了逗號,所以它顯示出來了
['11 24 62 59 1 28 61 5 54 38 31']
我一直在四處尋找,但找不到適合我確切問題的東西。
這是我的代碼:
import os
file_path = ("/Users/elian/Desktop/School/Scripting/Week 5/")
os.chdir(file_path)
file_name = ("Lab5.txt")
with open(file_name, "r ") as file:
contents = file.readlines() # Reads the contents of the file, outputs as a list.
print(type(contents)) # class 'list'
contents = ' '.join(str(contents).split(',')) # Removes commas
print(type(contents)) # class 'str'
我試過使用 split() 但它似乎只會讓一切變得更糟。
編輯:
這是該檔案正在讀取的數字生成器的源代碼:
import os
import random
# Set the file name
file_name = input("Please input your desired file name: ")
# Change the working directory
file_path = input("Please input the file path where this file is to be saved in: ")
os.chdir(file_path)
# Asks the user how many numbers they would like to generate.
num_of_values = int(input("How many random numbers would you like generated: "))
with open(file_name, "w") as file:
for i in range(1, num_of_values 1):
random_raw=random.randrange(0,100)
random_final=int(random_raw)
file.write
# convert to a string for the file write
num_string=str(random_final)
print(i)
if i < num_of_values:
num_string= num_string ","
file.write(num_string)
# Close the file - OS can have issues with files that are not closed
file.close()
uj5u.com熱心網友回復:
當您想找到十位時,您可以使用正則運算式:
l = ['11,24,62,59,1,28,61,5,54,38,31']
import re
out = ','.join(re.findall(r'\b[234]\d\b', l[0]))
輸出:'24,28,38,31'
正則運算式:
\b # word boundary
[234]\d # 2 digit number starting with 2 or 3 or 4
`b # word boundary
不包括下限:
l = ['11,20,24,62,59,1,28,61,5,54,38,31'] ## added 20 to the list
import re
','.join(re.findall(r'\b(2[1-9]|[34]\d)\b', l[0]))
輸出:'24,28,38,31'
正則運算式:
\b # word boundary
( # start group
2[1-9] # 2 digit number starting with 2 and ending in 1-9
| # OR
[34]\d # 2 digit number starting with 3 or 4
) # end group
`b # word boundary
uj5u.com熱心網友回復:
簡單的pythonic方式:
l = '11,24,62,59,1,28,61,5,54,38,31'
x = [i for i in l.split(",") if 20 < int(i) < 50]
print(x)
# ['24', '28', '38', '31']
uj5u.com熱心網友回復:
假設字串總是具有這種精確的形式:
list_string_in_list = ['11,24,62,59,1,28,61,5,54,38,31']
valid_int_list = [int(i) for i in list_string_in_list[0].split(",") if 20 < int(i) < 50]
或者,如果您更喜歡使用回圈:
list_string_in_list = ['11,24,62,59,1,28,61,5,54,38,31']
valid_int_list = []
for number in list_string_in_list[0].split(","):
number_as_int = int(number)
if 20 < number_as_int < 50:
valid_int_list.append(number_as_int)
但是,您構建此串列的方式很可能首先是錯誤的。如果您遍歷某些內容并創建此串列,為什么要將其附加到串列中的一個長字串中?.append()首先使用它來填寫該串列可能更有意義。
編輯:您正在閱讀的檔案中似乎給出了這種格式。因此,只需將其決議為字串然后拆分是正確的方法。
uj5u.com熱心網友回復:
要更有效地寫入檔案:
import random
with open('test.txt', 'w') as file:
num_list = random.choices(range(100), k=11)
num_string = ','.join(str(n) for n in num_list)
file.write(num_string)
或者如果允許使用該csv模塊:
import random
import csv
with open('test.txt', 'w', newline='') as file:
writer = csv.writer(file)
num_list = random.choices(range(100), k=11)
writer.writerow(num_list)
要讀取檔案:
with open('test.txt') as file:
contents = [int(n) for n in file.read().strip().split(',')]
print(contents)
或使用csv模塊:
import csv
with open('test.txt', newline='') as file:
reader = csv.reader(file)
for line in reader:
contents = [int(n) for n in line]
print(contents)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/430308.html
上一篇:在資料框中的串列物件中查找值
