开发者问题收集

将任意行的输入文件写入制表符分隔的输出[重复]

2020-09-22
507

我正在尝试编写一个脚本,该脚本将获取一个输入文件,该文件包含未知数量的列(用逗号分隔),并创建一个新文件(名称由用户指定),其中的列由制表符分隔。

我正在使用的测试输入文件如下所示:

Data 1,35,42,7.34,yellow,male
Data 2,41,46,8.45,red,female

这是我目前拥有的代码:

# Read input file
infile = open("input_file.txt", "r")

line_count = 0

# Read as a collection, removing end line character
for line in infile:
    print(line, end = "")
print("The input file contains", line_count, "lines.")

# Request user input for output file name
filename = input("Enter a name for the output file: ")

# Prompt for file name if entry is blank or only a space    
while filename.isspace() or len(filename) == 0:
    
    filename = input("Whoops, try again. Enter a name for the output file: ")

# Complete filename creation
filename = filename + ".txt"
filename = filename.strip()

# Write output as tab-delim file
for line in infile:
    outfile = open(filename, "w")
    outfile.write(line,"\t")
    outfile.close()

print("Success, the file", filename, "has been written.")
    
# Close input file
infile.close()

写入输出的部分不起作用 - 它不会产生错误,但输出为空白。

2个回答

您可以用逗号分隔行,并在添加制表符( \t )时书写:

with open('input_file.txt','r') as f_in, open('output_file.txt', 'w') as f_out:
    for line in f_in:
        s = line.strip().split(',')
        for i in s:
            f_out.write(i+'\t')
        f_out.write('\n')

或者按照@martineau 的建议简短书写:

with open('input.txt','r') as f_in, open('output.txt', 'w') as f_out:
    for line in f_in:
        s = line.strip().split(',')
        f_out.write('\t'.join(s) + '\n')
Barbaros Özhan
2020-09-22

您可以使用 pandas :

import pandas as pd
df = pd.read_csv("input_file.txt", sep=',',header=None)
print("The input file contains", df.shape[0], "lines.")
filename = input("Enter a name for the output file: ").strip()

# Prompt for file name if entry is blank or only a space    
while filename.isspace() or len(filename) == 0:
    filename = input("Whoops, try again. Enter a name for the output file: ")
    
#Saving to csv with | separator
df.to_csv(f'{filename}.txt', sep="\t", header=None, index=None)

print("Success, the file", filename, "has been written.")
Sebastien D
2020-09-22