Python: 在文本文件中编辑字符串时遇到问题
Python: trouble editing string in a text file
所以这让我发疯。我需要一个包含(全部在一行中)
的文本文件
mmfff m fm fmm FFF mmmmmm mfmfmf mmmfff MM fmmmf
并在程序中对其进行操作。它需要打开并读取文件,编辑掉空格,并将所有字母更改为大写。然后它需要打印编辑后的文件,计算m和f,并输出它们占整体的百分比。
# A program to determine male to female ratio
import math
main = raw_input("Enter the name of your file: ")
inputFile = open(main, 'r+')
gender =(inputFile.read().replace(" ", "").replace("f", "F").replace("m", "M"))
inputFile.close()
inputFile= open(main, 'r+')
inputFile.write(gender)
inputFile.close()
print gender
fletter = 0
mletter = 0
mletter = gender.count('M')
fletter = gender.count('F')
mletter =((mletter*100)/39)*1.0
fletter =((fletter*100)/39)*1.0
print "There are", mletter, " males and", fletter, " females."
试了很多方法,现在都想不起来了!我的问题是它没有正确编辑 txt 文件,不知何故我在字符串末尾有额外的字母。它拒绝在最后对我的数学进行四舍五入,所以我最终得到了 58 和 41,而它应该是 59。是的,我确实尝试了四舍五入函数,但没有帮助。
您这里有很多问题:
- 您正在进行整数除法,然后转换为浮点数。要解决此问题,请尝试:改为
mletter =((mletter*100)/39.0)
。
- 通过使用
r+
模式覆盖文件,您不会先删除旧内容。这意味着当您首先 运行 应用程序时,它将删除空格并仅用新内容覆盖开头,并保留最后几个字母不变。请尝试模式 w
。
- 无需像这样将内容设置为 0:
fletter = 0
无论如何,您只需在下一行覆盖它即可。
- 您没有使用
math
模块,无需导入它。
(结果顺便说一下是58.97435897435898,不是59)
我对下面的代码做了一些改进。内联注释解释了每个部分的作用以及它对您的初始尝试进行了哪些改进。
main = raw_input("Enter the name of your file: ")
# make this a function since we use the equation more than once
def get_percent(part, whole):
return 100 * float(part)/float(whole)
# make sure we handle the file not existing
try:
with open(main, 'r+') as fd:
# replace
newstr = fd.read().replace(' ', '').upper()
# after reading the file the file pointer will be at the end, we need to seek
# back to the start of the file
fd.seek(0)
# remove the existing file contents
fd.truncate()
fd.write(newstr)
except IOError:
# provide a friendly error and exit instead of a stacktrace
raise SystemExit("Failed to open file %s" % main)
mletter = newstr.count('M')
fletter = newstr.count('F')
total = len(newstr)
# Using string formatting is often easier to read than string concatentation
print "There are %s%% males and %s%% females" %(
get_percent(mletter, total),
get_percent(fletter, total)
)
所以这让我发疯。我需要一个包含(全部在一行中)
的文本文件mmfff m fm fmm FFF mmmmmm mfmfmf mmmfff MM fmmmf
并在程序中对其进行操作。它需要打开并读取文件,编辑掉空格,并将所有字母更改为大写。然后它需要打印编辑后的文件,计算m和f,并输出它们占整体的百分比。
# A program to determine male to female ratio
import math
main = raw_input("Enter the name of your file: ")
inputFile = open(main, 'r+')
gender =(inputFile.read().replace(" ", "").replace("f", "F").replace("m", "M"))
inputFile.close()
inputFile= open(main, 'r+')
inputFile.write(gender)
inputFile.close()
print gender
fletter = 0
mletter = 0
mletter = gender.count('M')
fletter = gender.count('F')
mletter =((mletter*100)/39)*1.0
fletter =((fletter*100)/39)*1.0
print "There are", mletter, " males and", fletter, " females."
试了很多方法,现在都想不起来了!我的问题是它没有正确编辑 txt 文件,不知何故我在字符串末尾有额外的字母。它拒绝在最后对我的数学进行四舍五入,所以我最终得到了 58 和 41,而它应该是 59。是的,我确实尝试了四舍五入函数,但没有帮助。
您这里有很多问题:
- 您正在进行整数除法,然后转换为浮点数。要解决此问题,请尝试:改为
mletter =((mletter*100)/39.0)
。 - 通过使用
r+
模式覆盖文件,您不会先删除旧内容。这意味着当您首先 运行 应用程序时,它将删除空格并仅用新内容覆盖开头,并保留最后几个字母不变。请尝试模式w
。 - 无需像这样将内容设置为 0:
fletter = 0
无论如何,您只需在下一行覆盖它即可。 - 您没有使用
math
模块,无需导入它。
(结果顺便说一下是58.97435897435898,不是59)
我对下面的代码做了一些改进。内联注释解释了每个部分的作用以及它对您的初始尝试进行了哪些改进。
main = raw_input("Enter the name of your file: ")
# make this a function since we use the equation more than once
def get_percent(part, whole):
return 100 * float(part)/float(whole)
# make sure we handle the file not existing
try:
with open(main, 'r+') as fd:
# replace
newstr = fd.read().replace(' ', '').upper()
# after reading the file the file pointer will be at the end, we need to seek
# back to the start of the file
fd.seek(0)
# remove the existing file contents
fd.truncate()
fd.write(newstr)
except IOError:
# provide a friendly error and exit instead of a stacktrace
raise SystemExit("Failed to open file %s" % main)
mletter = newstr.count('M')
fletter = newstr.count('F')
total = len(newstr)
# Using string formatting is often easier to read than string concatentation
print "There are %s%% males and %s%% females" %(
get_percent(mletter, total),
get_percent(fletter, total)
)