以上就是给各位分享python:从压缩的文本文件中读取行,其中也会对python读取压缩文件进行解释,同时本文还将给你拓展java–无法从文本文件中读取行、Python2.7-使用字典从文本文件中查找
以上就是给各位分享python:从压缩的文本文件中读取行,其中也会对python 读取压缩文件进行解释,同时本文还将给你拓展java – 无法从文本文件中读取行、Python 2.7-使用字典从文本文件中查找并替换为新的文本文件、python – 从文本文件中读取元组、python – 从文本文件中读取多个数字等相关知识,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:- python:从压缩的文本文件中读取行(python 读取压缩文件)
- java – 无法从文本文件中读取行
- Python 2.7-使用字典从文本文件中查找并替换为新的文本文件
- python – 从文本文件中读取元组
- python – 从文本文件中读取多个数字
python:从压缩的文本文件中读取行(python 读取压缩文件)
使用python从gz压缩的文本文件中读取一行很容易,而无需完全提取该文件?我有一个大约200mb的text.gz文件。当我提取它时,它变成7.4gb。这不是我必须阅读的唯一文件。对于整个过程,我必须读取10个文件。尽管这将是一个顺序的工作,但我认为在不影响全部信息的情况下做到这一点将是明智之举。我什至不知道有可能。如何使用python完成?我需要逐行阅读文本文件。
答案1
小编典典您是否尝试过使用gzip.GzipFile?参数类似于open
。
java – 无法从文本文件中读取行
我有这样的方法:
public int getIncrement() {
String extractFolder = "/mnt/sdcard/MyFolder";
boolean newFolder = new File(extractFolder).mkdir();
int counter = 0;
try {
File root = new File(extractFolder);
if (root.canWrite()){
File gpxfile = new File(root, "gpxfile.txt");
FileWriter gpxwriter = new FileWriter(gpxfile);
BufferedWriter out = new BufferedWriter(gpxwriter);
Scanner scanner = new Scanner(new FileInputStream(gpxfile.getAbsolutePath()), "UTF-8");
Log.i("PATH: ", extractFolder + "/gpxfile.txt");
while(scanner.hasNextLine()) {
String inc = scanner.nextLine();
counter = Integer.parseInt(inc);
Log.i("INSIDE WHILE: ", Integer.toString(counter));
}
counter++;
out.write(Integer.toString(counter));
out.close();
}
} catch (IOException e) {
Log.e("GEN_PCN: ", "Could not write file " + e.getMessage());
}
return counter;
}
我想要完成的是返回此文件的内容,并将整数递增1.但似乎我无法进入while循环,因为LogCat不打印任何内容.是的,我确定路径是正确的.
解决方法:
我想FileWriter gpxwriter的构造函数在创建Scanner时已经清空了文件,因此该文件为空并且hasNextLine返回false.为什么要在阅读时打开文件进行写作?
Python 2.7-使用字典从文本文件中查找并替换为新的文本文件
我是编程的新手,并且在过去的几个月中一直在业余时间学习python。我决定要尝试创建一个小的脚本,将文本文件中的美国拼写转换为英语拼写。
在过去的5个小时里,我一直在尝试各种事情,但最终想出了一些可以使我更加接近目标的东西,但还远远没有达到目标!
#imported dictionary contains 1800 english:american spelling key:value pairs. from english_american_dictionary import dictdef replace_all(text, dict): for english, american in dict.iteritems(): text = text.replace(american, english) return textmy_text = open(''test_file.txt'', ''r'')for line in my_text: new_line = replace_all(line, dict) output = open(''output_test_file.txt'', ''a'') print >> output, new_lineoutput.close()
我确信有更好的处理方法,但是对于此脚本,这是我遇到的问题:
- 在输出文件中,这些行每隔一行写入一行,并且之间有换行符,但是原始的test_file.txt没有此行。test_file.txt的内容显示在此页面底部
- 仅将一行中的美国拼写的第一个实例转换为英语。
- 我并不是很想在附加模式下打开输出文件,但是无法在此代码结构中找出“ r”。
任何对此急切的新人表示赞赏的帮助!
test_file.txt的内容为:
I am sample file.I contain an english spelling: colour.3 american spellings on 1 line: color, analyze, utilize.1 american spelling on 1 line: familiarize.
答案1
小编典典您看到的多余空白行是因为您print
要写出末尾已经包含换行符的行。由于也print
编写了自己的换行符,因此您的输出将变成双倍行距。一个简单的解决方法是使用outfile.write(new_line)
。
至于文件模式,问题在于您要一遍又一遍地打开输出文件。一开始,您只需要打开一次即可。使用with
语句来处理打开的文件通常是一个好主意,因为当您使用它们时,它们会为您关闭它们。
我不理解您的其他问题,仅发生了一些替换。是您的字典中失踪的拼写''analyze''
和''utilize''
?
我建议的一个建议是不要逐行更换。您可以一次读取整个文件file.read()
,然后将其作为一个单元进行处理。这可能会更快,因为它不需要在拼写字典中的项目上循环那么频繁(只需循环一次,而不是每行一次):
with open(''test_file.txt'', ''r'') as in_file: text = in_file.read()with open(''output_test_file.txt'', ''w'') as out_file: out_file.write(replace_all(text, spelling_dict))
编辑:
为了使您的代码正确处理包含其他单词的单词(例如包含“ tire”的“ entre”),您可能需要放弃使用str.replace
正则表达式的简单方法。
这是一个快速拼凑的解决方案,它使用re.sub
,给出了从美式到英式英语的拼写变化字典(即,与您当前字典相反的顺序):
import re#from english_american_dictionary import ame_to_bre_spellingsame_to_bre_spellings = {''tire'':''tyre'', ''color'':''colour'', ''utilize'':''utilise''}def replacer_factory(spelling_dict): def replacer(match): word = match.group() return spelling_dict.get(word, word) return replacerdef ame_to_bre(text): pattern = r''\b\w+\b'' # this pattern matches whole words only replacer = replacer_factory(ame_to_bre_spellings) return re.sub(pattern, replacer, text)def main(): #with open(''test_file.txt'') as in_file: # text = in_file.read() text = ''foo color, entire, utilize'' #with open(''output_test_file.txt'', ''w'') as out_file: # out_file.write(ame_to_bre(text)) print(ame_to_bre(text))if __name__ == ''__main__'': main()
关于此代码结构的一个好处是,如果您以其他顺序将字典传递给replacer_factory
函数,则可以轻松地将英式英语拼写转换回美式英语拼写。
python – 从文本文件中读取元组
这是我的txt:
(0,0) (0,0) (1,0) (2,3) (1,1) (1,1) (3,3) (2,2) (2,1) (4,4) (3,2) (3,1) (5,5)
我想逐一阅读这些列并获取元组列表:
尝试numpy我有这个:
import numpy as np File = np.genfromtxt('file.txt',delimiter=' ',dtype= tuple)
但它返回一个包含字节类型元素的列表列表.
显然我可以改变数据存储在txt中的方式.我只需要从txt获取元组列表(或列表列表).
解决方法
import re pattern='\((\d+,\d)\)' with open('demo.txt','r') as f: for line in f: data=re.findall(pattern,line) data_1=[] for item in data: data_1.append(tuple(map(lambda x:int(x),item.split(',')))) if data_1: print(data_1)
输出:
[(0,0),(0,(1,(2,3)] [(1,1),(3,3)] [(2,2),(4,4)] [(3,(5,5)]
甚至更好:
import re pattern='\((\d+,line) data_1=[tuple(map(lambda x:int(x),'))) for item in data] if data_1: print(data_1)
python – 从文本文件中读取多个数字
我有一个包含几个数字的文本文件:
12 35 21 123 12 15 12 18 89
我需要能够读取每行的单个数字,以便能够在数学公式中使用它们.
解决方法
with open("datafile") as f: for line in f: #Line is a string #split the string on whitespace,return a list of numbers # (as strings) numbers_str = line.split() #convert numbers to floats numbers_float = [float(x) for x in numbers_str] #map(float,numbers_str) works too
我已经完成了所有这一切的步骤,但你会经常看到人们组合它们:
with open('datafile') as f: for line in f: numbers_float = map(float,line.split()) #work with numbers_float here
最后,在数学公式中使用它们也很容易.首先,创建一个函数:
def function(x,y,z): return x+y+z
现在遍历你的文件调用函数:
with open('datafile') as f: for line in f: numbers_float = map(float,line.split()) print function(numbers_float[0],numbers_float[1],numbers_float[2]) #shorthand: print function(*numbers_float)
我们今天的关于python:从压缩的文本文件中读取行和python 读取压缩文件的分享就到这里,谢谢您的阅读,如果想了解更多关于java – 无法从文本文件中读取行、Python 2.7-使用字典从文本文件中查找并替换为新的文本文件、python – 从文本文件中读取元组、python – 从文本文件中读取多个数字的相关信息,可以在本站进行搜索。
本文标签: