0

I'm needing some help on appending my data to the output .csv file. What I have is I'm generating a .csv file down line by line from city and state data. It has to be in order from top to bottom and not random. Here is the code that is creating the .csv file with one column.

f_in = open("data_files/main_keyword.txt",'r') prefix = f_in.read().strip() f_in = open('data_files/states/' + random.choice(list(open('data_files/generate_what_state.txt'))).replace("\n", "") + '.csv', 'r') f_out = open('data_files/titles.csv', 'w') for line in f_in.readlines(): f_out.write(prefix.title() + ' ' + line.split(",")[1]+"" + ' ' + line.split(",")[2]+"" + ' - ' + random.choice(list(open('data_files/end_keywords.txt'))).replace("\n", "").title() + ' In ' + line.split(",")[0]+"" + '') f_out.write('\n') f_in.close() 

Example data of the output for the above code:

Air Duct Cleaning Addison Texas - Cleaning Air Ducts In Dallas Air Duct Cleaning Allen Texas - Cost Of Air Duct Cleaning In Dallas Air Duct Cleaning Balch Springs Texas - Cleaning Air Ducts In Dallas Air Duct Cleaning Carrollton Texas - Cleaning Air Ducts In Dallas 

That creates what I need for the city and state titles in the first column. What I need to do is append this data to the next data that is generated. So my first column is the city and state titles, my next column needs to be this data generated in the output file specified below in the code.

f_in = open("data_files/main_keyword.txt",'r') prefix = f_in.read().strip() f_in = open('data_files/states/' + random.choice(list(open('data_files/generate_what_state.txt'))).replace("\n", "") + '.csv', 'r') f_out = open('import_this_file_into_wordpress.csv', 'w') for line in f_in.readlines(): f_out.write(';<p>' + random.choice(list(open('data_files/content.txt'))).replace("\n", "") + '</p>' + '\n') f_in.close() 

Example data of the output for the above code:

<p>This is a line of text generated.</p> <p>This is a line of text generated.</p> <p>This is a line of text generated.</p> <p>This is a line of text generated.</p> 

So what I'm doing is generating the city and state titles and saving them into a .csv file and then generating the body of the post and needing to append the city and state titles to the first column of the file import_this_file_into_wordpress.csv. I need to append the city and state titles from top to bottom entirely line by line to this file. So my final output would be column 1 is city and state title and column 2 is the body content. Can someone help me with this? I hope I made some sense of that. If not please tell me and I will try and clarify a bit more. Thank you for your help.

Example of how the data should be output in the final file:

Air Duct Cleaning Addison Texas - Cleaning Air Ducts In Dallas,<p>This is a line of text generated.</p> Air Duct Cleaning Allen Texas - Cost Of Air Duct Cleaning In Dallas,<p>This is a line of text generated.</p> Air Duct Cleaning Balch Springs Texas - Cleaning Air Ducts In Dallas,<p>This is a line of text generated.</p> Air Duct Cleaning Carrollton Texas - Cleaning Air Ducts In Dallas,<p>This is a line of text generated.</p> 
2
  • Question would be clearer if you showed an example of what the input file looks like and what the desired output should be. Commented Mar 7, 2014 at 19:38
  • I updated the question with some example data. Commented Mar 7, 2014 at 19:47

1 Answer 1

1

Given that you have already written your data to two files, you can read those files from disk and zip every line together like this:

from itertools import izip file1 = 'data_files/titles.csv' file2 = 'import_this_file_into_wordpress.csv' dest = 'combined.csv' with open(file1) as f1, open(file2) as f2, open(dest, 'w') as dest: for [part1, part2] in izip(f1, f2): dest.write("%s,%s" % (part1.rstrip(), part2)) 

By using with you can open up all three files (two input files and one destination file) at the same time and when the block ends all of them will be closed again.

The for loop will in each iteration take one line from the first input file and one line from the second input file, combine them together with a comma between, and append it to the output file. The rstrip removes the newlines from the lines of the first file so that a combined line is not broken into two lines.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you first for your response. I am using Python 3.3.2. Is there an issue with import izip in version 3.3.2? I'm getting an error of can't import izip.
Yeah, sorry I forgot to comment on that. My code assumes python 2. If you're using Python 3 then zip works as itertools.izip by default. Just remove the import on the first line, and change izip to zip.
Perfect! Thank you Michael, you have found a solution to my problem. Great work! It is working perfectly now.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.