Reputation: 1695
You guys were super helpful with my last newbie question so I figured I would give it another shot. Right now, my Python 3 code takes data from a CSV file, stores the data as a list, creates permutations of that data, and exports it back out into a new csv file in a column format. Unfortunately, the format of the exported list is not the best for what I need it for. My question is: How do I remove the commas, brackets, and parenthesis from the exported list in python?
How it Looks Now:
('Hello', 'World', 'How')
('Hello', 'World', 'Are')
('Hello', 'How', 'World')
How I would like it to look:
Hello World How
Hello World Are
Hello How World
Here is the code I am using, does anyone have any advice or suggestions? I am trying to learn so any extra insight would be much appreciated!
import csv
import itertools
with open('/users/Desktop/pythondata.csv', 'r') as f:
reader = csv.reader(f)
J = []
for row in reader:
J.extend(row)
C = list(itertools.permutations(J, 3))
with open('THISISATEST.csv','w')as f:
writer=csv.writer(f,lineterminator='\n')
for val in C:
writer.writerow([val])`
P.S. -- Please forgive me if this is not formatted correctly, I am new to the board!
Upvotes: 4
Views: 14925
Reputation: 33
If anyone is still searching for this as I was when try this, (python 3.x). My aim was to list on separate lines all possible permutations of a 9 digit number.
the line string = ''.join(str(char) for char in line)
was the winner
import sys, itertools
f = open('path to file', 'w') #I was using a .txt file
permutations = (list(itertools.permutations([1,2,3,4,5,6,7,8,9], 9)))
for line in permutations:
string = ''.join(str(char) for char in line)
f.write(string + '\n')
f.close()
Upvotes: 0
Reputation: 12928
I recommend that you just use the join
method to get the single string you are looking for. You could try:
with open('test.csv', 'w') as a_file:
for result in C:
result = ' '.join(result)
a_file.write(result + '\n')
Basically, the join
method called on a the string ' '
(a single space) will take each item in its argument (a single result from your permutations) and add them together in a single string separated by whatever that first string was (the single space). This site gives a simple, illustrative example.
The join
method is called with the syntax '[separator]'.join([list of items to join])
, so if you just wanted to mash everything together into one string with no spaces, you could call it on an empty string: ''.join([my list])
.
Since you do not need a comma-separated file (CSV), you can simplify things by just using the a_file.write
method as shown. If you use the CSV writer in this code you will get commas between every letter.
Upvotes: 3
Reputation: 663
Since you have tuples, there's an easier way. You don't need csv
to export a space-delimited file.
import csv
import itertools
with open('pythondata.csv', 'r') as f:
reader = csv.reader(f)
J = []
for row in reader:
J.extend(row)
C = itertools.permutations(J, 3)
with open('THISISATEST.txt','w') as f:
f.write('\n'.join(' '.join(y for y in x) for x in C))
Upvotes: 1
Reputation: 2078
I don't know if you are aware of regular expressions but using python's 're' module should give you what you are looking for:
import re
strs = "('Hello', 'World', 'How')"
#creates list of strings that match the regular expression
words = re.findall(r"\'([A-Za-z]+)\'", strs)
#joins all of the elements of the word array into a string separated by a space
outputString = " ".join(words)
#output is "Hello World How"
print outputString
Upvotes: 2