1

I am using this pdf to csv function from {Python module for converting PDF to text} and I was wondering how can I now export the result to a csv file on my drive? I tried adding in the function

with open('C:\location', 'wb') as f:
    writer = csv.writer(f)
    for row in data:
        writer.writerow(row)

but the resulting csv file has one character per row and not the rows I have when printing data in python.

4
  • 2
    What is the type of data? If it's a string, you are just iterating over characters and indeed writing one character per line. Commented Oct 14, 2016 at 10:48
  • it is a string. Isn't there a way into which I can just create a csv with the exact result of the data without iterating Commented Oct 14, 2016 at 10:55
  • It all depends on the format of data. If it is in rows, you could try splitting it by the line separator lines=data.split("\n"). Additionaly, you have to pass a sequence to writer.writerow. See docs.python.org/3.4/library/csv.html#writer-objects Commented Oct 14, 2016 at 10:59
  • @AndreiCozma Note that backslashes are escape characters so you probably want a double backslash here to insert a literal one: with open('C:\location', 'wb') as f: Commented Oct 14, 2016 at 11:01

1 Answer 1

1

If you are printing a single character per row, then what you have is a string. Your loop

for row in data:

translates to

for character in string:

so you need to break your string up into the chunks you want written on a single row. You might be able to use something like data.split() but it's hard to say without seeing more of your code and data.

In response to your comment: yes, you can just dump the data to a CSV... If it adheres to the rules of CSV. If your data is separated by commas, with each row terminated by a newline, then you can just write your data to a file.

with open ("file.csv",'w') as f:
  f.write(data)

This will ONLY work if your data adheres to the rules of csv.

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

8 Comments

It would be very difficult to pass here the entire result as there are 195 pages of it. Tour solution for the entire csv did work Bravosierra99, but I think the data doesn't adhere to therules of csv. For ex this 3 rows from print data:
SMC CORPORATION;(6273);67,369,359;0 SHINKAWA LTD.;(6274);20,047,500;0 NAVITAS CO.,LTD.;(6276);5,722,500;0 In my csv file are :
spread over 5 columns and not the new column doesn't start where the ';' is in the data. Apologies for the 3 comments
so what issues are you still having? Is the data not going into the proper rows?
the rows are correct I think, but I just do not have the columns delimited by the ";" which means that some cells contain data that I can not use
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.