Python 使用 Python 将文本文件重新格式化为 csv 的脚本
Python script for reformatting a text file into a csv using Python
我被要求阅读包含以下内容的文本文件:
1.
Wicked Stepmother (1989) as Miranda
A couple comes home from vacation to find that their grandfather has …
2.
Directed By William Wyler (1988) as Herself
During the Golden Age of Hollywood, William Wyler was one of the …
3.
Whales of August, The (1987) as Libby Strong
Drama revolving around five unusual elderly characters, two of whom …
4.
As Summers Die (1986) as Hannah Loftin
Set in a sleepy Southern Louisiana town in 1959, a lawyer, searches …
并创建如下所示的 .csv 输出文件:
1,Wicked Stepmother ,1989, as Miranda,A couple comes home from vacation …
2,Directed By William Wyler ,1988, as Herself,During the Golden Age of …
3,"Whales of August, The ",1987, as Libby Strong,Drama revolving around five…
我知道,如果我可以将这些行分开,那么我可以将它们重新组合在一起,并在它们之间添加逗号,然后将这些字符串写入我的输出文件。我的问题是格式。对于我只想要的数字:
line1=stringname[0]+','
line2= stringname[:stringname.find('(')-1]+','+stringname[stringname.find('(')+1:stringname.find(')')-1]+','+stringname[stringname.find(')')+1:]
不修改第 3 行然后写入文件
result=line1+line2+line3
问题是我不知道在任何给定时间我正在解析哪一行。我在想也许是 for 循环中的某些东西,它确保我一次以 3 行为一组解析代码,但我不确定如何同时管理文件处理。我也不确定如何防止循环越过程序的末尾。
这可以使用正则表达式轻松完成,但我猜您不希望使用它。
相反,可以通过一次读取一行文件并确定该行是否以数字开头后跟 .
来解决问题。如果是,请开始构建行列表,直到找到下一个数字。
使用 Python 的 int()
函数将尝试将字符串转换为数字。 find('.')
函数尝试定位数字的末尾。
如果返回的字符串不是数字,则会引发 ValueError
异常。在这种情况下,将该行添加到行列表中。
如果有数字,首先将任何现有条目写入 csv
文件,然后开始一个新条目。
最后,不会有最终的数字行来触发下一次写入,因此添加另一个调用以将最后一行写入 csv。
例如:
import csv
with open('text.txt') as f_input, open('output.csv', 'wb') as f_output:
csv_output = csv.writer(f_output)
entry = []
for line in f_input:
line = line.strip() # Remove the trailing newline
if len(line): # Does the line containing anything?
try:
number = int(line[:line.find('.')])
if len(entry):
csv_output.writerow(entry)
entry = [number]
except ValueError:
entry.append(line)
csv_output.writerow(entry)
Python 的 csv
库用于获取列表并在写入 csv 输出文件时自动在条目之间添加必要的逗号。如果条目包含逗号,它会自动添加引号。
我被要求阅读包含以下内容的文本文件:
1.
Wicked Stepmother (1989) as Miranda
A couple comes home from vacation to find that their grandfather has …
2.
Directed By William Wyler (1988) as Herself
During the Golden Age of Hollywood, William Wyler was one of the …
3.
Whales of August, The (1987) as Libby Strong
Drama revolving around five unusual elderly characters, two of whom …
4.
As Summers Die (1986) as Hannah Loftin
Set in a sleepy Southern Louisiana town in 1959, a lawyer, searches …
并创建如下所示的 .csv 输出文件:
1,Wicked Stepmother ,1989, as Miranda,A couple comes home from vacation …
2,Directed By William Wyler ,1988, as Herself,During the Golden Age of …
3,"Whales of August, The ",1987, as Libby Strong,Drama revolving around five…
我知道,如果我可以将这些行分开,那么我可以将它们重新组合在一起,并在它们之间添加逗号,然后将这些字符串写入我的输出文件。我的问题是格式。对于我只想要的数字:
line1=stringname[0]+','
line2= stringname[:stringname.find('(')-1]+','+stringname[stringname.find('(')+1:stringname.find(')')-1]+','+stringname[stringname.find(')')+1:]
不修改第 3 行然后写入文件
result=line1+line2+line3
问题是我不知道在任何给定时间我正在解析哪一行。我在想也许是 for 循环中的某些东西,它确保我一次以 3 行为一组解析代码,但我不确定如何同时管理文件处理。我也不确定如何防止循环越过程序的末尾。
这可以使用正则表达式轻松完成,但我猜您不希望使用它。
相反,可以通过一次读取一行文件并确定该行是否以数字开头后跟 .
来解决问题。如果是,请开始构建行列表,直到找到下一个数字。
使用 Python 的 int()
函数将尝试将字符串转换为数字。 find('.')
函数尝试定位数字的末尾。
如果返回的字符串不是数字,则会引发 ValueError
异常。在这种情况下,将该行添加到行列表中。
如果有数字,首先将任何现有条目写入 csv
文件,然后开始一个新条目。
最后,不会有最终的数字行来触发下一次写入,因此添加另一个调用以将最后一行写入 csv。
例如:
import csv
with open('text.txt') as f_input, open('output.csv', 'wb') as f_output:
csv_output = csv.writer(f_output)
entry = []
for line in f_input:
line = line.strip() # Remove the trailing newline
if len(line): # Does the line containing anything?
try:
number = int(line[:line.find('.')])
if len(entry):
csv_output.writerow(entry)
entry = [number]
except ValueError:
entry.append(line)
csv_output.writerow(entry)
Python 的 csv
库用于获取列表并在写入 csv 输出文件时自动在条目之间添加必要的逗号。如果条目包含逗号,它会自动添加引号。