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.

Community
  • 1
  • 1
Andrei Cozma
  • 950
  • 3
  • 9
  • 14
  • 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. – Sven Rusch Oct 14 '16 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 – Andrei Cozma Oct 14 '16 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 https://docs.python.org/3.4/library/csv.html#writer-objects – Sven Rusch Oct 14 '16 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:` – dkasak Oct 14 '16 at 11:01

1 Answers1

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.

bravosierra99
  • 1,331
  • 11
  • 23
  • 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: – Andrei Cozma Oct 14 '16 at 12:19
  • 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 : – Andrei Cozma Oct 14 '16 at 12:19
  • spread over 5 columns and not the new column doesn't start where the ';' is in the data. Apologies for the 3 comments – Andrei Cozma Oct 14 '16 at 12:22
  • so what issues are you still having? Is the data not going into the proper rows? – bravosierra99 Oct 14 '16 at 12:35
  • 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 – Andrei Cozma Oct 14 '16 at 12:51
  • perhaps I don't understand the data.. Are you telling me that a ';' is supposed to be the delimiter? – bravosierra99 Oct 14 '16 at 12:58
  • well then, you have a couple of options. First off, most software you can tell it what delimeter you want to use. If you google this, you can figure out how to do it. Or you can use `str.replace()` aka `data.replace(';',',')` which would replace all of your ';' with ','. NOTE: this only works properly if you have no ',' in your data, which is not True for you. So you are going to need to replace those with a different character FIRST, then repalce your semicolons. Then everything should be peachy. – bravosierra99 Oct 14 '16 at 13:05
  • if I helped at all, I would appreciate an up arrow or accepting my solution. Thanks. – bravosierra99 Oct 14 '16 at 14:57