官术网_书友最值得收藏!

  • Julia Cookbook
  • Jalem Raj Rohit
  • 227字
  • 2021-07-08 11:12:45

Handling data with TSV files

In this section, we will explain how to handle Tab Separated Values (TSV) files.

Getting ready

The DataFrames package is needed to deal with TSV files. So, as it is already installed as instructed in the previous section, we can move ahead and make sure that all the packages are up-to-date with the following command:

Pkg.update()

How to do it...

TSV files, as the name suggests, are files whose contents are separated by commas. TSV files can be accessed and read into the REPL process by the following method:

  1. Assign a variable to the local source directory of the file:
    s = "/Users/username/dir/data.tsv"
    
  2. The readtable() command is used to read the data from the source. The data is read in the form of a Julia DataFrame:
    data = readtable(s)
    

Data can be written to TSV files from a Julia DataFrame using the following steps:

  1. Create a data structure with some data inside it. For example, let's create a two-dimensional dataframe like the one we created in the previous example:
    using DataFrames
    df = DataFrame(A = 1:10, B = 11:20)
    
  2. Now, the dataframe, which we created in Step 1, can be exported to an external TSV file using the following command:
    writetable("data.csv",df)
    

The writetable() command is clever enough to make out the format of the file from the filename extension.

主站蜘蛛池模板: 义马市| 平安县| 武安市| 常熟市| 宿松县| 临猗县| 万年县| 达拉特旗| 临猗县| 杭州市| 镇远县| 江门市| 乐平市| 油尖旺区| 永靖县| 赣州市| 贵阳市| 宁蒗| 稻城县| 呼伦贝尔市| 五常市| 赞皇县| 德格县| 云林县| 莱西市| 贵定县| 海淀区| 远安县| 昭平县| 保靖县| 观塘区| 淄博市| 扶沟县| 神池县| 大连市| 五大连池市| 龙州县| 随州市| 大足县| 韶关市| 驻马店市|