I have a file, each line looks like this:
#each line is a list:
a = ['1\t2\t3\t4\t5']
#type(a) is list
#str(a) shows as below:
["['1\\t2\\t3\\t4\\t5']"]
I want an output of only 1 2 3 4 5, how should I accomplish that? Thanks everyone.
If you are reading lines from a file, you can use the following approach:
with open("file.txt", "r") as f_input:
for line in f_input:
print line.replace("\t"," "),
If the file you are reading from is actually columns of data, then the following approach might be suitable:
import csv
with open("file.txt", "r") as f_input:
csv_input = csv.reader(f_input, delimiter="\t")
for cols in csv_input:
print " ".join(cols)
cols would give you a list of columns for each row in the file, i.e. cols[0] would be the first column. You can then easily print them out with spaces as shown.
[i.replace('\t',' ') for i in a]?ast.literal_eval()?