with open('somefile.txt') as f:
lines = f.readlines()
print(lines)
vals = []
for line in lines:
vals.append(line.rpartition(':')[2].strip()) <-- this would basically split your string by colon
row = ",".join(vals) <-- this is the one you can use
print(row)
with open('file1.txt') as f_in:
lines = (line for line in f_in)
with open('csv_out.txt', 'w') as f_out:
for line_num, line in enumerate(lines, start=1):
# Obtain required data without any white space
line = line.strip().split(':')[1][1:]
if line_num % 3 == 0:
# On every 3rd line add a newline; else add the required comma between the fields
line += '\n'
else:
line += ','
f_out.write(line)
import pandas as pd
# Read in the text file
df = pd.read_csv("input.txt", delimiter='\t')
# Make any necessary edits to the DataFrame
# Export the DataFrame to a CSV file
df.to_csv("output.csv", index=False)
3条答案
按热度按时间von4xj4u1#
你可以试试这样的方法:
输出:
['姓名:无名氏\n','年龄:30\n','职务:会计师']
无名氏30岁会计
您也可以将其写入csv,如下所示:
qyyhg6bp2#
我假设您希望CSV文件中的每条记录后面都有一个换行符,所以这应该可以实现:
我还假设你的源文件和你在问题中陈述的完全一样,但是,如果你 * 没有 * 冒号之间的空格,只需从第6行删除[1:]切片。
在第二行中使用生成器解析意味着不需要将整个源文件加载到内存中,这意味着源文件有多大并不重要(这可能与您相关,也可能无关)。
如果出于某种原因(现在或以后)需要将源代码保存在内存中,可以将第二行替换为:
xn1cxnb43#