用什么语言来操作文本文件 [英] What language to manipulate text files

查看:89
本文介绍了用什么语言来操作文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对许多文件进行一些棘手的文本文件处理,但是只需要一点点编程知识就可以了。


什么是理想的语言以下示例?


1.从某个文件夹开始,在子文件夹中查找匹配* FOOD * .txt的所有

文件名。文件夹应该

被复制到当前文件夹中名为EATING的新子文件夹

,新名称为* FOOD * COPY.txt


2.按如下方式处理每个文件:

这是我想要的输入和输出的简化示例。


----- --------------------------------输入

......... .................''几行不明文本文件

从苹果商店买苹果

从橘子里取橙子商店

从李子店买李子

从梨店买梨子

从苹果,橘子吃,

李子梨,'空白在行的开头是不重要的

..........................''更多未知的文本文件行

第1章

这里有关于苹果的几行文字

第2章

这里有关于橘子的几行文字

第3章

这里有关于李子的几行文字

第4章

关于梨的几行文字在这里


------------------------------------ - 输出

..........................''几行不明文本文件

从苹果商店购买苹果

从橙子店购买橙子

从李子店购买李子

从梨店买梨子

从百吉饼店购买百吉饼''获取线......

从甜甜圈店购买甜甜圈''可以任何顺序

从苹果,百吉饼中食用,橘子,

李子,甜甜圈,梨子在开始时的空白是不重要的

................. ......... '更多未知行的文字文件

第1章

关于苹果的几行文字

第2章

这里有关于百吉饼的几行文字

第3章

这里有关于橘子的几行文字

第4章

这里有关于李子的几行文字

第5章

这里有关于甜甜圈的几行文字

第6章
这里有关于梨的几行文字


摘要:

我已经添加了两个新项目来获取;

我在搜索了一个

特定的水果之后将它们放入逗号分隔的列表中,然后将每个水果放入其中;

这些章节重新编号以匹配它们在

以逗号分隔的列表。

几行文字关于每个新项目可以从

new_foods.txt文件(或bagels.txt和donuts.txt文件)中提取。


我的第一个目标是按照描述处理文件。

我的第二个目标是学习这种文本的最佳语言

操作。该语言应该在Windows 98,XP和Linux上运行。


Python会是最好的,还是像AutoHotKey这样的宏脚本式的东西?b $ b工作?

我想过Perl,但我想我会学习坏习惯并且很难读取代码。


谢谢,Ross

我想做一些棘手的事情。

解决方案

文本文件操作很多文件,但只有一点编程知识。
[...]

Python会是最好的,还是会像AutoHotKey这样的宏脚本工作?
我想到了Perl,但我想我会学习坏习惯并且难以阅读代码。




Perl和Python都非常擅长这类工作。这是
几乎激发了Perl的灵感,而Python实现了大部分相同的

工具集。您将使用常规

表达式解决许多此类问题。 (Perl中内置的第一类对象,使用re模块从
Python中的字符串创建。


当然不奇怪我会选择Python。主要是因为它提供了超出正则表达式的
。使用Python中的字符串方法可以处理许多简单的情况

(查看库参考的

内置部分中的序列类型信息 - 同时查看 ;字符串模块,

虽然通常更容易使用字符串方法方法。


你可能最终会得到更易读的代码Python和

花费更少的时间来培养足够的熟练程度来完成工作。

-

Terry Hancock(hancock at anansispaceworks.com )

Anansi Spaceworks http://www.anansispaceworks.com


为什么人们一直在询问

Python新闻组中某些语言的用语?显然答案是有偏见的。


不是因为我爱Python而不是坏事,但它没有意义

如果你真的想要一个客观的意见。


R


ross写道:

我想做一些棘手的文字对许多文件进行文件操作,但
只有一点编程知识。

以下示例的理想语言是什么?

1.从某个开始文件夹,在子文件夹中查找匹配* FOOD * .txt的所有文件名。每个文件夹中匹配的任何文件都应该被复制到当前文件夹中名为EATING
的新子文件夹中,并带有一个新名称of * FOOD * COPY.txt

2.按如下方式处理每个文件:
以下是我想要的输入和输出的简化示例。

- ------------------------------------输入
......... ................''几行未知的文本文件
从中获取苹果苹果店
从橘子店买橘子
从李子店买李子
从梨店买梨子
从苹果,橘子,李子,梨子的空白处吃行的起点并不重要
.........................''更多未知行的文字文件
第1章
关于苹果的几行文字在这里
第2章
关于这里有关于橘子的几行文字
第3章
关于李子的几行文字在这里
第4章
关于梨的几行文字

---------------------------- ---------输出
.........................''几行未知的文本文件从苹果商店购买苹果
从橙子店购买橙子
从李子店购买李子
从梨店购买梨子
从百吉饼店购买百吉饼''获取线...
从甜甜圈店买甜甜圈''可以任意顺序
从苹果,百吉饼,橘子,李子,甜甜圈吃s,pears''开头的空格是不重要的
.........................''更多未知的文本文件行
第1章
关于苹果的几行文字在这里
第2章
关于百吉饼的几行文字在这里
第3章
几行文字关于橘子在这里
第4章
这里有关于李子的几行文字
第5章
这里有关于甜甜圈的几行文字
第6章
几个关于梨的文本行

摘要:
我添加了两个新项目来获取;
我在搜索了一个将特定的水果放在后面;
章节重新编号以匹配其在逗号分隔列表中的位置。
几行文字。关于每个新项目可以从
new_foods.txt文件(或bagels.txt和donuts.txt文件)中提取。

我的第一个目标是按照描述处理文件。
我的第二个目标是学习这种文本操作的最佳语言。该语言应该在Windows 98,XP和Linux上运行。

Python会是最好的,还是会像AutoHotKey这样的宏脚本工作?
我想关于Perl,但是我想我会学习坏习惯,并且很难阅读代码。

谢谢,Ross



ross写道:

我想对许多文件进行一些棘手的文本文件操作,但只有一点编程知识。

什么是理想的语言对于以下示例?

1.从某个文件夹开始,在子文件夹中查找匹配* FOOD * .txt的所有文件名。每个文件夹中匹配的任何文件都应该是
复制到当前文件夹中名为EATING
的新子文件夹,其新名称为* FOOD * COPY.txt


这应该可以帮助您入门:


导入错误

来自路径导入路径# http://www.jorendorff.com/articles/python/path/

dst_dirpath = path(" EATING")


#create dst_dirpath

试试:

dst_dirpath.makedirs()#make目的地目录及其父母

除了OSError,错误:#error!

如果err.errno = errno.EEXIST:#可能只是它已经存在

如果不是dst_dirpath.isdir( ):#和它是一个目录

引发#如果没有,引发异常


for pathpath in path("。")。walkfiles (" * FOOD * .txt"):

infile = file(filepath)

outfile = file(dst_dirpath.joinpath(filepath.namebase +" _COPY .txt" ;))


...在这里处理...

我的第一个目标是按照描述处理文件。
我的第二个目标是学习这种文本操作的最佳语言。该语言应该在Windows 98,XP和Linux上运行。

Python会是最好的,还是像AutoHotKey这样的宏脚本工作?




就个人而言,我会使用Python,但你在这里问的是什么?

-

Michael Hoffman


I want to do some tricky text file manipulation on many files, but have
only a little programming knowledge.

What are the ideal languages for the following examples?

1. Starting from a certain folder, look in the subfolders for all
filenames matching *FOOD*.txt Any files matching in each folder should
be copied to a new subfolder within the current folder called EATING
with a new name of *FOOD*COPY.txt

2. Process each file as follows:
Here is a simplified example of what I want as input and output.

------------------------------------- input
.......................... ''several unknown lines of text file
Get apples from apples shop
Get oranges from oranges shop
Get plums from plums shop
Get pears from pears shop
Eat from apples, oranges,
plums, pears ''whitespace at start of line is unimportant
.......................... ''more unknown lines of text file
Chapter 1
Several lines of text about apples in here
Chapter 2
Several lines of text about oranges in here
Chapter 3
Several lines of text about plums in here
Chapter 4
Several lines of text about pears in here

------------------------------------- output
.......................... ''several unknown lines of text file
Get apples from apples shop
Get oranges from oranges shop
Get plums from plums shop
Get pears from pears shop
Get bagels from bagels shop ''the Get lines...
Get donuts from donuts shop ''can be in any order
Eat from apples, bagels, oranges,
plums, donuts, pears ''whitespace at start of line is unimportant
.......................... ''more unknown lines of text file
Chapter 1
Several lines of text about apples in here
Chapter 2
Several lines of text about bagels in here
Chapter 3
Several lines of text about oranges in here
Chapter 4
Several lines of text about plums in here
Chapter 5
Several lines of text about donuts in here
Chapter 6
Several lines of text about pears in here

Summary:
I have added two new items to Get;
I have put them into the comma-delimited list after searching for a
particular fruit to put each one after;
The Chapters are renumbered to match their position in the
comma-delimited list.
The "several lines of text" about each new item can be pulled from a
new_foods.txt file (or a bagels.txt and a donuts.txt file).

My first objective is to process the files as described.
My second objective is to learn the best language for this sort of text
manipulation. The language should run on Windows 98, XP and Linux.

Would Python be best, or would a macro-scripting thing like AutoHotKey
work?
I thought about Perl, but think I would learn bad habits and have hard
to read code.

Thanks, Ross

解决方案

On Saturday 11 June 2005 11:37 pm, ross wrote:

I want to do some tricky text file manipulation on many files, but have
only a little programming knowledge. [...]
Would Python be best, or would a macro-scripting thing like AutoHotKey
work?
I thought about Perl, but think I would learn bad habits and have hard
to read code.



Both Perl and Python are *extremely* good at this kind of work. This is
pretty much what inspired Perl, and Python implements most of the same
toolset. You will solve many of these kinds of problems using "regular
expressions" (built-in first-class object in Perl, created from strings in
Python using the "re" module).

No surprise of course that I would choose Python. Mainly because of what
it provides beyond regular expressions. Many simple cases can be handled
with string methods in Python (check the Sequence types information in the
built-ins section of the Library Reference -- also look at the "string" module,
though it''s usually easier to use the string methods approach).

You will probably end up with more readable code using Python and
take less time to develop sufficient proficiency to do the job with it.
--
Terry Hancock ( hancock at anansispaceworks.com )
Anansi Spaceworks http://www.anansispaceworks.com


Why do people keep asking what language to use for certain things in the
Python newsgroup? Obviously the answer is going to biased.

Not that it''s a bad thing because I love Python, but it doesn''t make sense
if you honestly want an objective opinion.

R

ross wrote:

I want to do some tricky text file manipulation on many files, but
have only a little programming knowledge.

What are the ideal languages for the following examples?

1. Starting from a certain folder, look in the subfolders for all
filenames matching *FOOD*.txt Any files matching in each folder should
be copied to a new subfolder within the current folder called EATING
with a new name of *FOOD*COPY.txt

2. Process each file as follows:
Here is a simplified example of what I want as input and output.

------------------------------------- input
......................... ''several unknown lines of text file
Get apples from apples shop
Get oranges from oranges shop
Get plums from plums shop
Get pears from pears shop
Eat from apples, oranges,
plums, pears ''whitespace at start of line is unimportant
......................... ''more unknown lines of text file
Chapter 1
Several lines of text about apples in here
Chapter 2
Several lines of text about oranges in here
Chapter 3
Several lines of text about plums in here
Chapter 4
Several lines of text about pears in here

------------------------------------- output
......................... ''several unknown lines of text file
Get apples from apples shop
Get oranges from oranges shop
Get plums from plums shop
Get pears from pears shop
Get bagels from bagels shop ''the Get lines...
Get donuts from donuts shop ''can be in any order
Eat from apples, bagels, oranges,
plums, donuts, pears ''whitespace at start of line is unimportant
......................... ''more unknown lines of text file
Chapter 1
Several lines of text about apples in here
Chapter 2
Several lines of text about bagels in here
Chapter 3
Several lines of text about oranges in here
Chapter 4
Several lines of text about plums in here
Chapter 5
Several lines of text about donuts in here
Chapter 6
Several lines of text about pears in here

Summary:
I have added two new items to Get;
I have put them into the comma-delimited list after searching for a
particular fruit to put each one after;
The Chapters are renumbered to match their position in the
comma-delimited list.
The "several lines of text" about each new item can be pulled from a
new_foods.txt file (or a bagels.txt and a donuts.txt file).

My first objective is to process the files as described.
My second objective is to learn the best language for this sort of
text manipulation. The language should run on Windows 98, XP and
Linux.

Would Python be best, or would a macro-scripting thing like AutoHotKey
work?
I thought about Perl, but think I would learn bad habits and have hard
to read code.

Thanks, Ross



ross wrote:

I want to do some tricky text file manipulation on many files, but have
only a little programming knowledge.

What are the ideal languages for the following examples?

1. Starting from a certain folder, look in the subfolders for all
filenames matching *FOOD*.txt Any files matching in each folder should
be copied to a new subfolder within the current folder called EATING
with a new name of *FOOD*COPY.txt
This should get you started:

import errno
from path import path # http://www.jorendorff.com/articles/python/path/

dst_dirpath = path("EATING")

# create dst_dirpath
try:
dst_dirpath.makedirs() # make destination directory and its parents
except OSError, err: # error!
if err.errno = errno.EEXIST: # might just be that it already exists
if not dst_dirpath.isdir(): # and it''s a directory
raise # if not, raise an exception

for filepath in path(".").walkfiles("*FOOD*.txt"):
infile = file(filepath)
outfile = file(dst_dirpath.joinpath(filepath.namebase+"_COPY .txt"))

...do processing here...
My first objective is to process the files as described.
My second objective is to learn the best language for this sort of text
manipulation. The language should run on Windows 98, XP and Linux.

Would Python be best, or would a macro-scripting thing like AutoHotKey
work?



Personally, I''d use Python, but what do you expect when you ask here?
--
Michael Hoffman


这篇关于用什么语言来操作文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆