Python: 从文件中读取标记然后使用标记得到 class
Python: Reading marks from a file then using marks to get class
我收到了 sheet,但遇到了问题。 The Task
这是我的记事本 txt:maths_mark
我想像这样创建它:
- 标记 => 90 = 10M1
- 标记 => 80 = 10M2
- 标记 => 70 = 10M3
- 标记 => 60 = 10M4
- 标记 => 50 = 10M5
- 标记 => 40 = 10M6
- 标记 => 30 = 10M7
- 标记 => 20 = 10M8
- 标记 => 10 = 10M9
我不知道如何从文件中读取标记,然后创建一个包含名称、标记和 class 的新文本文件。
如果有人能提供帮助,我们将不胜感激。
谢谢,Noob Coder
A bit of google can help here 该文章提供了打开和写入文件所需的一切。让你走上正确的道路:
您只需调用 f=open("fileName.txt","w+")
即可为您打开现有文件并将其存储到一个变量中。然后你可以 file_line=f.readlines()
一次抓取一行。根据需要操纵结果。使用操作的结果重新创建一个新的字符串变量 result= "My data"
,然后使用 f2=open("newFileName.txt","w+")
和一个新的文件名来创建一个新文件,您将在该文件上使用 f2.write(result)
以写入文件。确保关闭我们的文件 f.close(),f2.close()
数据操作部分由您决定!
I am stuck on how to read the marks from the file then creating a new
text file with the names, marks and class.
fileList = list()
with open("Maths_Marks", 'r') as fp:
fp = fp.readlines()
for line in fp:
tmpLine = line.split(",")
sname = tmpLine[0]
fname = tmpLine [1]
marks = tmpLine[2]
#Write your own logic to get GroupNumber.
#Append into line using
#line = line.strip() + "," + GroupNumber
#fileList.append(line.strip())
#Open new file Maths_Group and write into that
with open("Maths_Group",'w') as s:
for line in filelist:
print >> s, line
"line" will give you "Jack,Daniels,90"
line = line.strip() + "," + GroupNumber => will give you Jack,Daniels,90,10M1
如果要提取每个字段,请使用 else 保留下面的部分。
sname = tmpLine[0]
fname = tmpLine[1]
marks = tmpLine[2]
希望对您有所帮助。
import csv
# function to process data
def process_data(data):
for item in data:
marks = int(item[2])
mclass = 'incorrect data entry try again'
if marks == 0:
mclass = "10.9"
elif marks >= 0 and marks <= 100:
mclass = "10.{}".format(10 - int(item[2][0]))
yield '"{}","{}",{},{}'.format(item[0],item[1],item[2],mclass)
# read data to memory
data = []
with open("Maths_Mark.txt", "r") as csv_in:
data = list(csv.reader(csv_in, delimiter=','))
# write contents to file
with open('Maths_Group.txt', 'w') as csv_out:
for line in process_data(data):
csv_out.write(line + "\n")
我收到了 sheet,但遇到了问题。 The Task
这是我的记事本 txt:maths_mark 我想像这样创建它:
- 标记 => 90 = 10M1
- 标记 => 80 = 10M2
- 标记 => 70 = 10M3
- 标记 => 60 = 10M4
- 标记 => 50 = 10M5
- 标记 => 40 = 10M6
- 标记 => 30 = 10M7
- 标记 => 20 = 10M8
- 标记 => 10 = 10M9
我不知道如何从文件中读取标记,然后创建一个包含名称、标记和 class 的新文本文件。
如果有人能提供帮助,我们将不胜感激。
谢谢,Noob Coder
A bit of google can help here 该文章提供了打开和写入文件所需的一切。让你走上正确的道路:
您只需调用 f=open("fileName.txt","w+")
即可为您打开现有文件并将其存储到一个变量中。然后你可以 file_line=f.readlines()
一次抓取一行。根据需要操纵结果。使用操作的结果重新创建一个新的字符串变量 result= "My data"
,然后使用 f2=open("newFileName.txt","w+")
和一个新的文件名来创建一个新文件,您将在该文件上使用 f2.write(result)
以写入文件。确保关闭我们的文件 f.close(),f2.close()
数据操作部分由您决定!
I am stuck on how to read the marks from the file then creating a new text file with the names, marks and class.
fileList = list()
with open("Maths_Marks", 'r') as fp:
fp = fp.readlines()
for line in fp:
tmpLine = line.split(",")
sname = tmpLine[0]
fname = tmpLine [1]
marks = tmpLine[2]
#Write your own logic to get GroupNumber.
#Append into line using
#line = line.strip() + "," + GroupNumber
#fileList.append(line.strip())
#Open new file Maths_Group and write into that
with open("Maths_Group",'w') as s:
for line in filelist:
print >> s, line
"line" will give you "Jack,Daniels,90"
line = line.strip() + "," + GroupNumber => will give you Jack,Daniels,90,10M1
如果要提取每个字段,请使用 else 保留下面的部分。
sname = tmpLine[0]
fname = tmpLine[1]
marks = tmpLine[2]
希望对您有所帮助。
import csv
# function to process data
def process_data(data):
for item in data:
marks = int(item[2])
mclass = 'incorrect data entry try again'
if marks == 0:
mclass = "10.9"
elif marks >= 0 and marks <= 100:
mclass = "10.{}".format(10 - int(item[2][0]))
yield '"{}","{}",{},{}'.format(item[0],item[1],item[2],mclass)
# read data to memory
data = []
with open("Maths_Mark.txt", "r") as csv_in:
data = list(csv.reader(csv_in, delimiter=','))
# write contents to file
with open('Maths_Group.txt', 'w') as csv_out:
for line in process_data(data):
csv_out.write(line + "\n")