问题描述
我在写入文件时遇到了一些困难。设置是我在一个文件中有一些数据。它有一些整数、单个单词、句子(如地址等)、小数和其他(例如日期)。我想将数据分成几个类别,并将每个类别写入一个单独的文件。这是我到目前为止所管理的:
def data_reading():
#document_reading.close() = open("lines.txt","r",encoding="utf-8")
document_writing_integers = open("integers.txt","w",encoding="utf-8")
#document_writing_others = open("decimals.txt",encoding="utf-8")
document_writing_letters = open("words.txt",encoding="utf-8")
#document_writing_sentences = open("sentences.txt",encoding="utf-8")
data = []
integers = []
letters = []
sentences = []
others = []
with open("lines.txt",encoding="utf-8") as file:
for row in file:
row = row.strip().replace("\n","")
data.extend(row.split(","))
for value in data:
if value.isnumeric():
#print(value + " - integer")
integers.append(value)
document_writing_integers.write(value + "\n")
elif value.isalpha():
letters.append(value.upper())
document_writing_letters.write(value + "\n")
#print(value + " - alphabetical strings")
elif value.endswith("."):
sentences.append(value.lower().capitalize())
#print(value + " - sentence")
#document_writing_sentences.write(value + "\n")
elif value.count(".") == 1:
others.append(value)
#document_writing_others.write(value + "\n")
#print(value + " - float")
else:
print("None of the above '" + value + "'.")
break
#document_reading.close()
document_writing_integers.close()
#document_writing_others.close()
document_writing_letters.close()
#document_writing_sentences.close()
return integers,letters,sentences,others
def sentence_writing(full_sentences):
document_writing_sentences = open("sentences.txt",encoding="utf-8")
textline = sentences
for items in textline:
document_writing_sentences.write(items + "\n")
document_writing_sentences.close()
return textline
def writing_others(last):
document_writing_others = open("decimals.txt",encoding="utf-8")
decimals = others
for rows in decimals:
document_writing_sentences.write(rows + "\n")
document_writing_others.close()
return decimals
def the_main():
integers,others = data_reading()
#textline = sentence_writing(sentences)
#decimals = writing_others(last)
the_main()
#integers,others = data_reading()
#print(integers)
#print(sentences)
#print(letters)
#print(others)
我无法写入文件。 data_reading() 的输出看起来不错,我用 print() 查了一下。所以整数是例如 [12,23923,1,etc],小数是 [1.3 67.1],句子包含 [Address unkNown for example],其他是日期,例如 22.04.78。由于某种原因,小数和句子根本没有写入文件,单词也不是全部大写。主要问题是我的句子和其他文件写入功能有问题,我还没有找到原因。任何反馈表示赞赏。
编辑:我的帖子有点冗长和压倒性。我想我缩小了问题的范围,或者至少我希望如此。如果我只看我的写入文件功能:
def sentence_writing(full_sentences):
document_writing_sentences = open("virke.txt",encoding="utf-8")
textline = modified_sentences
#while True:
print(textline)
for items in textline:
document_writing_sentences.write(items + "\n")
document_writing_sentences.close()
return textline
从 print() 我得到 ['Sunday service.','Adriano chilentano.','I buy american.'] - 如我所愿,首字母大写,其余小写。所以我想我有兴趣知道将其写入文件的最佳方式。我用正常的方式做这件事时运气不好,while 循环。
解决方法
由于我写入文件函数时参数错误,代码无法正常工作。可行的解决方案是:
def data_reading():
data = []
integers = []
letters = []
sentences = []
others = []
with open("data_file.txt") as file:
for row in file:
row = row.strip().replace("\n","")
data.extend(row.split(","))
for value in data:
if value.isnumeric():
integers.append(value)
elif value.isalpha():
letters.append(value.upper())
elif value.endswith("."):
sentences.append(value.lower().capitalize())
elif value.count(".") == 1:
others.append(value)
else:
print("Data not recognized '" + value + "'.")
break
return integers,letters,sentences,others
def sentence_writing(full_sentences):
document_writing_sentences = open("complete_sentences.txt","w",encoding="utf-8")
textline = full_sentences
for items in textline:
if items == "0":
break
else:
document_writing_sentences.write(items + "\n")
document_writing_sentences.close()
return None
def writing_others(last):
document_writing_others = open("decimals_file.txt",encoding="utf-8")
decimals = last
for items in decimals:
if items == "0":
break
else:
document_writing_others.write(items + "\n")
document_writing_others.close()
return None
def writing_words(single_word):
document_writing_letters = open("solitary_words.txt",encoding="utf-8")
words = single_word
for items in words:
if items == "0":
break
else:
document_writing_letters.write(items + "\n")
document_writing_letters.close()
return None
def writing_integers(integers):
document_writing_integers = open("integers_extracted.txt",encoding="utf-8")
for items in integers:
if items == "0":
break
else:
document_writing_integers.write(items + "\n")
document_writing_integers.close()
return None
def main():
integers,others = data_reading()
writing_integers(integers)
single_word = letters
writing_words(single_word)
last = others
writing_others(last)
full_sentences = sentences
sentence_writing(full_sentences)
return None
main()