-1

I have this txt delimited by whitespaces sinheader.txt . How can I make a script that reads this txt and with it write a csv file.

This is the script Im trying to implement:

import csv
import pandas as pd
prueba = open('station.info').readlines()
open('sinheader.txt', 'w').writelines(prueba[4:-1])
with open('sinheader.txt', 'r') as in_file:
    lines = (line.split("  ") for line in in_file)
    with open('malobueno.txt', 'w') as out_file:
        writer = csv.writer(out_file)
        writer.writerows(lines)

This is what Im getting:Bad.txt

  • The output file looks like a csv file. What is the 'Bad' part you want to fix? – balki Sep 29 '17 at 18:30
  • Don't post links to images, try to post everything as text in the question itself. It is helpful if you give a sample of the input file, and an *example of what you are looking for as an output*. To me, though, it seems like `sinheader.txt` is tab-delimited. – juanpa.arrivillaga Sep 29 '17 at 18:30
  • What is the specific problem you're having? "Please write this for me" is not a good fit for a StackOverflow question. – Henry Keiter Sep 29 '17 at 18:38

2 Answers2

1

In pandas you can use a regular expression in the delimiter. That is assuming you don't mind reading the file in using pandas.

import pandas as pd
filename = 'station.info'
lines = pd.read_csv(filename, sep='[\s]{2,}',header=None)

The regular expression searches for two or more spaces. So this will only work if your input file uses two spaces for separation and the text fields use single spaces.

You could then uses pandas.DataFrame.to_csv to save the data in csv format.

Flynnstone
  • 106
  • 5
0

I believe that this problem comes from your use of split(" "). Since there are many spaces after the name "AACR", the function considers it as if it were many columns.

This answer might help you. The split() function is used without arguments to separate strings with any number of whitespaces.

I hope it helps.

  • Additionally, it might be helpful to consider her data looks tab-delimited, in which case it should be `.split('\t')` – Mangohero1 Sep 29 '17 at 19:03